user2974793
user2974793

Reputation: 13

Regex - match string between quotes but also what precedes it

If I have a file that I need to search through for every occurrence of this:

firstName="string"

I know how to match the string between the quotes, but I would like to know how to match the variations of what precedes it. Like:

firstName ="string"
firstName = "string"

So, basically, I need to get all the strings, but I encountered a problem when there are variations like that, with added spaces before or after =. I'm sure this is really simple, but I'm really bad at regex so I would appreciate your help. Thank you in advance for your answer.

Upvotes: 1

Views: 86

Answers (1)

plalx
plalx

Reputation: 43718

I do not know about C# explicitely, however something like the following should work:

(\w+)\s*=\s*"(.*?)"

(\w+) matches a block of alphanumeric characters an creates a capturing group

\s* matches 0 or more spacing characters

= matches =

\s* matches 0 or more spacing characters

"(.*?)" matches "anything" and creates a capturing group over what's inside ""

I guess you need to make sure the global flag is enabled to get all matches.

Upvotes: 1

Related Questions