tags:

views:

262

answers:

3

Hi, I have a snippet looking something like the below.

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'd like use regular expression in C# to get it to:

string bodyTypeAssemblyQualifiedName = null;

I've tried using a RegEx like the below but it doesn't match the newlines ...

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

You can try

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

Or you set the RegexOptions.Singleline for your pattern.

RegexOptions.Singleline - Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

Tomalak
+1  A: 

This works:

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

Which is the equivalent of:

  1. After the string "string bodyTypeAssemblyQualifiedName = "
  2. Turn on single line (treat \r\n as any other character) ( this is what (?s: ) does)
  3. match every character that is not a semicolon
  4. until a single semicolon is reached
Will
+1  A: 

@Will, by replacing the dot with a negated character class, you eliminated the need for the single-line modifier. And if I'm reading the question right, you don't need to use lookarounds either.

bodyTypeAssemblyQualifiedName\s+=\s+(?<location>[^;]+);
Alan Moore
My regex doesn't require you use any code other than Regex.Replace(input, pattern, replacement).
Will