Regex to detect if a text is between two parenthesis (herringbones) with no space after and before

Viewed 47
"a <a bc> de"     # TRUE
"a <<a bc>> de"   # TRUE
"a <a bc> >de"    # TRUE
"a <<a bc> de"    # TRUE
"a < a bc> de"    # FALSE
"a <a bc > de"    # FALSE
"a <<a bc >> de"  # FALSE
"a <a bc >> de"   # FALSE

I tried the following one :

regex = "<+\S.*\S]>+" 
1 Answers

Converting my comment to answer so that solution is easy to find for future visitors.

You may use this regex:

<+[^\s<](?:[^<>]*[^\s<>])?>+

RegEx Demo

RegEx Breakup:

  • <+: Match 1+ of < characters
  • [^\s<]: Match any char that is not a whitespace and not a <
  • (?:: Start non-capture group
    • [^<>]*: Match 0 or more of any char that is not < and >
    • [^\s<>]: Match any char that is not a whitespace and not < and >
  • )?: End non-capture group. ? makes this group optional
  • >+: Match 1+ of > characters
Related