Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:59:34+00:00 2026-06-14T02:59:34+00:00

I want to write php regular expression to find uppercase string , which can

  • 0

I want to write php regular expression to find uppercase string , which can also contain one number and spaces, from text.

For example from this text "some text to contain EXAM PL E 7STRING uppercase word" I want to get string- EXAM PL E 7STRING ,

found string should start and end only with uppercase, but in the middle, without uppercase letters can also contain(but not necessarily ) one number and spaces. So, regex should match any of these patterns

1) EXAMPLESTRING               - just uppercase string
2) EXAMP4LESTRING              - with number
3) EXAMPLES TRING              - with space
4) EXAM PL E STRING            - with more than one spaces
5) EXAMP LE4STRING             - with number and space
6) EXAMP LE 4ST RI NG          - with number and spaces 

and with total length string should be equal or more than 4 letters

I wrote this regex '/[A-Z]{1,}([A-Z\s]{2,}|\d?)[A-Z]{1,}/', that can find first 4 patterns, but I can not figure it out to match also the last 2 patterns.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:59:35+00:00Added an answer on June 14, 2026 at 2:59 am

    There is a neat trick called a lookahead. It just checks what is following after the current position. That can be used to check for multiple conditions:

    '/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])(?!(?:[A-Z\s]*\d){2})[A-Z][A-Z\s\d]*[A-Z]/'
    

    The first lookaround is actually a lookbehind and checks that there is no previous uppercase letter. This is just a little speedup for strings that would fail the match anyway. The second lookaround (a lookahead) checks that there are at least four letters. The third one checks that there are no two digits. The rest just matches then a string of the allowed characters, starting and ending with an uppercase letter.

    Note that in the case of two digits this will not match at all (instead of matching everything up to the second digit). If you do want to match in such a case, you could incorporate the “1 digit” rule into the actual match instead:

    '/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])[A-Z][A-Z\s]*\d?[A-Z\s]*[A-Z]/'
    

    EDIT:

    As Ωmega pointed out, this will cause problems if there are less then four letters before the second digit, but more after that. This is actually quite tough, because the assertion needs to be, that there are more than 4 letters before the second digit. Since we do not know where the first digit occurs in those four letters, we have to check for all possible positions. For this I would do away with the lookaheads altogether, and simply provide the three different alternatives. (I will keep the lookbehind as an optimization for non-matching parts.)

    '/(?<![A-Z])[A-Z]\s*(?:\d\s*[A-Z]\s*[A-Z]|[A-Z]\s*\d\s*[A-Z]|[A-Z]\s*[A-Z][A-Z\s]*\d?)[A-Z\s]*[A-Z]/'
    

    Or here with added comments:

    '/
    (?<!         # negative lookbehind
        [A-Z]    # current position is not preceded by a letter
    )            # end of lookbehind
    [A-Z]        # match has to start with uppercase letter
    \s*          # optional spaces after first letter
    (?:          # subpattern for possible digit positions
        \d\s*[A-Z]\s*[A-Z]
                 # digit comes after first letter, we need two more letters before last one
    |            # OR
        [A-Z]\s*\d\s*[A-Z]
                 # digit comes after second letter, we need one more letter before last one
    |            # OR
        [A-Z]\s*[A-Z][A-Z\s]*\d?
                 # digit comes after third letter, or later, or not at all
    )            # end of subpattern for possible digit positions
    [A-Z\s]*     # arbitrary amount of further letters and whitespace
    [A-Z]        # match has to end with uppercase letter
    /x'
    

    That gives the same result on Ωmega’s lengthy test input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a regular expression in PHP. From this code I want
I want to write the regular expression in php matching condition below: /* Remove
i want a write a php script which can start , stop , enable
I want to write a php code that can search for multiple keywords separated
I need help on regular expression here. I want PHP to be able to
Possible Duplicate: Regular Expression Sanitize (PHP) I want to make my links SEO. Before
I'm new to php and am trying to write a regular expression using preg_match
How to use php preg_replace and regular expression to remove all the hyperlink which
i wrote some php code that can change any width=any number with the string
I want to write a php a function which will create 50000 unique random

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.