Remove quotes from quoted string using regex

2019-08-06 02:57发布

问题:

I have a string like

FVAL(XXX)="TRUE" AND FVAL(TT)="FALSE" 

I want to replace all "TRUE" and "FALSE" by TRUE AND FALSE.

Now the resultant string should be

FVAL(XXX)=TRUE AND FVAL(TT)=FALSE

Will the code shown below be upto the mark for this.

Regex.Replace("FVAL(XXX)=""TRUE"" AND FVAL(TT)=""FALSE""", "[""]TRUE[""]", "TRUE", RegexOptions.IgnoreCase)

Note: Now I know you guys would say that this is not a constructive question and should be closed, but the reason I asked this is because what I came up with, will have to be written twice once for TRUE and once for FALSE, which is not the desired result, instead I want the regex to find and replace just once. Also I have to be absolutely sure that my regex would not miss out any pattern. And lastly if you think this is not constructive enough, then please go ahead and close it.

回答1:

USe

"(TRUE|FALSE)"

and replace it by

$1

In VB.NET this becomes

ResultString = Regex.Replace(SubjectString,
    """(TRUE|FALSE)\""", "$1", 
    RegexOptions.Singleline)

Let us know if you have any more questions