Home > database >  REGEX doesn't match string if substring has already been matched
REGEX doesn't match string if substring has already been matched

Time:09-22

As seen here, the keyword Social Media was already matched so Social Media Wall is not being matched, Although it's different with the case of ERP as ERP and ERP Corecon both are matched.

Input Text -

hey, G Suite , ,Social Media Wall, Social Media Wall , ERP ERP Corecon

Query -

((?<=[,-./\s])(ERP Corecon|Social Media|Social Media Wall|ROI|ERP)\b)

Output -

hey, G Suite , ,**Social Media** Wall, **Social Media** Wall , **ERP** **ERP Corecon**

Ideal Output - Social Media & Social Media Wall both should be matched

regex link - https://regexr.com/6ugr5

CodePudding user response:

You may be able to use this regex:

(?<=[,-./\s])(ERP(?: Corecon)?|Social Media(?: Wall)?|ROI)\b

RegEx Demo

RegEx Breakup:

  • (?<=[,-./\s]): Lookbehind to assert presence of these chars before current position
  • (; Start capture group
    • ERP(?: Corecon)?: Match ERP or ERP Corecon
    • |: OR
    • Social Media(?: Wall)?: Match Social Media or Social Media Wall
    • |: OR
    • ROI: Match ROI
  • ): End capture group
  • \b: Word boundary

CodePudding user response:

You could try to use named groups:

(?<SocialMediaWall>(?<SocialMedia>Social Media)( Wall)?)|(?<ERPCore>(?<ERP>ERP)( Corecon)?)|(?<ROI>ROI)

As you can see, the SocialMedia group is nested inside the SocialMediaWall group. That's a way of getting both values.

  • Related