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

  • SEARCH
  • Home
  • 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 527641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:54:17+00:00 2026-05-13T08:54:17+00:00

The text string I want to validate consists of what I call segments. A

  • 0

The text string I want to validate consists of what I call “segments”. A single segment might look like this:

 [A-Z,S,3]

So far I managed to build this regex pattern

(?:\[(?<segment>[^,\]\[}' ]+?,[S|D],\d{1})\])+?

it works but it will return matches even though the whole text string contains invalid text. I guess I need to use ^ and $ somewhere in my pattern but I can’t figure out how!?

I would like my pattern to produce the following results:

  • [A-Z,S,3][A-Za-z0-9åäöÅÄÖ,D,4] OK(two segments)
  • [A-Z,S,3]aaaa[A-Za-z0-9åäöÅÄÖ,D,4] No match
  • crap[A-Z,S,3][A-Za-z0-9åäöÅÄÖ,D,4] No match
  • [A-Z,S,3][] No match
  • [A-Z,S,3][klm,D,4][0-9,S,1] OK(three segments)
  • 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-05-13T08:54:17+00:00Added an answer on May 13, 2026 at 8:54 am

    Use ^ to anchor the start and $ to anchor the end. E.g.: ^(abc)*$, this matches zero or more repetitions of the group (“abc” in this example) and that must start at the start of the input string and end at the end of it.

    ^(?:[(?[^,][}' ]+?,[S|D],\d{1})])+$—using an ungreedy +? doesn’t matter, as you require it to match until the end anyway. However, your regex has a few issues.

    ^(?:\[[^,]+,[SD],\d\])+$—seems more like what you want.

    • I couldn’t decipher what you meant by the first part, so my regex is more general than required, [^,]+, will match any sequence of non-commas followed by a comma, and in fact you should probably add ] to this negated character class.
    • [S|D] is a character class of three characters, as | doesn’t mean alternation here ((S|D) would mean the same as [SD] though).
    • {1} is the default for any atom, you don’t need to specify it.

    Pseudocode (run it at codepad.org):

    import re
    def find_segments(input_string):
      results = []
      regex = re.compile(r"\[([^],]+),([SD]),(\d)\]")
      start = 0
      while True:
        m = regex.match(input_string, start)
        if not m: # no match
          return None # whole string didn't match, do another action as appropriate
        results.append(m.group(1, 2, 3))
        start = m.end(0)
        if start == len(input_string):
          break
      return results
    
    print find_segments("[A-Z,S,3][klm,D,4][0-9,S,1]")
    # output:
    #[('A-Z', 'S', '3'), ('klm', 'D', '4'), ('0-9', 'S', '1')]
    

    The big difference here is the expression matches only the complete [...] part, but it is applied in succession, so they must start again where the last ends (or end at the end of the string).

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

Sidebar

Related Questions

i want a regular expression to validate string to have only text,operators and these
I have this string: Test: String And I want to select the text before
I have a string of text like so: var foo = FooBar; I want
I want to validate a text string for a specific value: All numeric except
I want to check an input string to validate a proper text. a. I
Situation is a string that results in something like this: <p>This is some text
i have a text file with string abcdef I want to search for the
I want to find all img tags in a string of text and put
I want to convert a string to formatted text in C# in WPF application,
I want to bind a string value to a text box but only if

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.