Warm tip: This article is reproduced from serverfault.com, please click

Regex pattern to replace string containing special characters

发布于 2021-01-12 07:44:00

Need help finding out the regex pattern that will help me replacing below -

*LABEL and it’s value which contains within double quote. (Text within double quotes may contain all different special characters and there is no fix pattern)

I am giving 2 different examples -

Input String 1 -

*QUESTION 99924 *ALPHA 1026L3 *DUMMY *VAR “version” *LABEL “SCRIPT VERSION” *PROPERTIES “ReportType=IsShell;ExcludeFromDAU=True”

Expected Output String -

*QUESTION 99924 *ALPHA 1026L3 *DUMMY *VAR “version” *PROPERTIES “ReportType=IsShell;ExcludeFromDAU=True”

Input String 2 -

*QUESTION 270 *CODES 4705L996 *RANDOM *MULTI *MIN 1 *LABEL “BARRIERS_TO_ATTENDENCE - B3” *VAR “BARRIERS_TO_ATTENDENCE” *PROPERTIES “DIMVAR=BARRIERS_TO_ATTENDENCE” *IF [#Q180,1] UIOPTIONS "? ROWPICKER"

Expected Output String -

*QUESTION 270 *CODES 4705L996 *RANDOM *MULTI *MIN 1 *VAR “BARRIERS_TO_ATTENDENCE” *PROPERTIES “DIMVAR=BARRIERS_TO_ATTENDENCE” *IF [#Q180,1] UIOPTIONS "? ROWPICKER"
Questioner
SonuD
Viewed
0
Andreas Christiansen 2021-01-12 16:27:25
\*LABEL\s?“([^”])*”

The above regex will match any occurrence of *LABEL followed by an optional whitespace, then your opening character and anything except your closing character, ”, and finally the closing character, ”.

If you need to remove the space after the label-flag, you can extend the regex like so:

\*LABEL\s?“([^”])*”\s?

NOTE, the capture group (marked by the parenthesis) is optional and can be reduced to \*LABEL\s?“[^”]*”\s?.