RegEx will replace value including newlines

I have a snippet that looks something like this.

string bodyTypeAssemblyQualifiedName = "XXX.XX.XI.CustomerPayment.Schemas.r1.CustomerPayments_v01, XXX.XX.XI.CustomerPaym" +
                "ent.Schemas.r1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ac564f277cd4488" +
                "e";

      

I would like to use a regex in C # to get it:

string bodyTypeAssemblyQualifiedName = null;

      

I tried using RegEx as shown below but it doesn't match newlines ...

bodyTypeAssemblyQualifiedName\s=\s(?<location>.*?);

      

0


source to share


3 answers


It works:

(<= string \ sbodyTypeAssemblyQualifiedName \ c = \ c?) (C: [^;] *) (? =;)



Which is equivalent:

  • After the line "string bodyTypeAssemblyQualifiedName ="
  • Include one line (treat \ r \ n like any other character) (this is what (? S :) does)
  • matches every character that is not a semicolon
  • until one semicolon is reached.
+1


source


@Will, by replacing dot with a negative character class, you have eliminated the need for a one-line modifier. And if I'm reading the question correctly, you don't need to use images either.



bodyTypeAssemblyQualifiedName\s+=\s+(?<location>[^;]+);

      

+1


source


You may try

bodyTypeAssemblyQualifiedName\s=\s(?<location>[.\n]*?);

      

Or you install RegexOptions.Singleline

for your template.

RegexOptions.Singleline . Indicates single line mode. Modifies the value of the period (.) To match every character (instead of every character except \ n).

0


source







All Articles