Regex is a match string that: and

I have below the possible lines and I need to match specific characters.

Possible string to match:

dsg.1.2.3.4.5.6.7.5 = STRING: 1234 blah blah blah

OR

dsg.1.2.3.4.5.6.7.5 = STRING: "1234 blah blah blah"

Below is the regex I tried. It works, but for the first line, it matches "

\=\s*STRING\:\s(?=\")\"([^"]*)|([^:]*$)

To match the line above, I used a condition that is well suited for dsg.1.2.3.4.5.6.7.5 = STRING: "1234 blah blah blah"

Not dsg.1.2.3.4.5.6.7.5 = STRING: 1234 blah blah blah

Exit after the match:

2.  [29-58] ` 1234 blah blah blah`
Is required

:

1.  [29-58] `1234 blah blah blah` --> No space 

Please help me in this matter.

+4
source share
2 answers

In perl (PCRE), you can use a regular expression using the (?|...)non-captureuring group construct :

/=\s*STRING:\s(?|"([^"]*)|([^:]*))/

RegEx Demo

(?|...) - Subpatterns declared as part of each alternative to this construct will start from the same index.

1234 blah blah blah # 1 .

+2

:

=\s*STRING:\s("?)([^"]*)\1

, ("?) ( ), . STRING $2.

+7

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


All Articles