ewolfman
ewolfman

Reputation: 361

Regex substring with delimiter

I have the following text (could be infinite number of delimited pairs):

zz=yy /* animal=cat,AA=bb, qqq=dd */ yyy=ttt

I would like to use regex to get all matches of the delimited strings within the code comment:

1. animal=cat
2. AA=bb
3. qqq=dd

but not: zz=yy or yyy=ttt

Update: I have tested the suggested patterns (all good, I learnt a lot, thanks). I continued to experiment and found the following pattern:

(?:\/\*\s*|\G\s*,\s*)(?:(\w+)=(\w+)(?:\s*\*\/)?)

Which takes less steps and also handles multiline. Do you see any problem with this solution?

https://regex101.com/r/YfC4dS/1/

Upvotes: 1

Views: 1103

Answers (2)

CertainPerformance
CertainPerformance

Reputation: 370769

One option is

(\b\w+)=(\w+\b)(?=(?:(?!\/\*).)*\*\/)

In plain language, what that means is: after the = pair, lookahead for (characters that don't include the /* substring), followed by */.

https://regex101.com/r/Dvx99F/1

An alternative that takes fewer steps, matching from the beginning of the /*s instead:

(?:\/\*|\G(?!^))(?:(?!\*\/|\/\*).)*?(\b\w+)=(\w+\b)

https://regex101.com/r/Dvx99F/2

Upvotes: 2

Jan
Jan

Reputation: 43169

If your engine supports \G, you may use

(?:\G(?!\A)|/\*)
(?:(?:(?!\*/)[\s\S])+?)
(?P<key>\w+)=(?P<value>\w+)

See a demo on regex101.com.


Explained:

(?:\G(?!\A)|/\*)             # match /* or at the end of the last match
(?:(?:(?!\*/)[\s\S])+?)      # fast-forward
(?P<key>\w+)=(?P<value>\w+)  # key/value pair

Upvotes: 1

Related Questions