Regex matches the text after the specified character, with the exception of the character itself

Consider the following text -

[Event "F/S Return Match"] 

I want to extract -

 F/S Return Match 

Now I use -

 \"(.*)" 

What yeilds are -

 "F/S Return Match" 

Then I use -

 [^"]* 

To obtain -

 F/S Return Match 

Can I combine two into one?

+4
source share
1 answer

Look-around might be an option:

 (?<=")[^"]*(?=") 

(?<=") checks that the previous character is " .
(?=") checks that the next character is " .

Test .

An alternative is simply grouping :

 "([^"]*)" 

How to extract a group depends on the language used.

Test . (note the "Matching groups" area)

I didn’t just use "(.*)" Because the string abc "def" "ghi" would match "def" "ghi" , although you might want to match "def" and "ghi" separately. An alternative to [^"] is an undesired match - "(.*?)" , Which will match the string as little as possible.

+3
source

Source: https://habr.com/ru/post/1489307/


All Articles