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

Time:09-16

"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]> " 

CodePudding user response:

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