Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline regular expression in C# [duplicate]

Tags:

c#

regex

How do I match and replace text using regular expressions in multiline mode?

I know the RegexOptions.Multiline option, but what is the best way to specify match all with the new line characters in C#?

Input:

<tag name="abc">this is a text</tag> 

Output:

[tag name="abc"]this is a test [/tag] 

Aahh, I found the actual problem. '&' and ';' in Regex are matching text in a single line, while the same need to be escaped in the Regex to work in cases where there are new lines also.

like image 413
Priyank Bolia Avatar asked Nov 22 '09 21:11

Priyank Bolia


People also ask

What is multiline in regular expression?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

How do I add a line break in regex?

Line breaks If you want to indicate a line break when you construct your RegEx, use the sequence “\r\n”. Whether or not you will have line breaks in your expression depends on what you are trying to match.

How do you match everything including newline regex?

The dot matches all except newlines (\r\n). So use \s\S, which will match ALL characters.

What is multiline pattern match?

Pattern. MULTILINE or (? m) tells Java to accept the anchors ^ and $ to match at the start and end of each line (otherwise they only match at the start/end of the entire string). Pattern.


2 Answers

If you mean there has to be a newline character for the expression to match, then \n will do that for you.

Otherwise, I think you might have misunderstood the Multiline/Singleline flags. If you want your expression to match across several lines, you actually want to use RegexOptions.Singleline. What it means is that it treats the entire input string as a single line, thus ignoring newlines. Is this what you're after...?

Example

Regex rx = new Regex("<tag name=\"(.*?)\">(.*?)</tag>", RegexOptions.Singleline); String output = rx.Replace("Text <tag name=\"abc\">test\nwith\nnewline</tag> more text...", "[tag name=\"$1\"]$2[/tag]"); 
like image 181
David Hedlund Avatar answered Sep 18 '22 21:09

David Hedlund


Here's a regex to match. It requires the RegexOptions.Singleline option, which makes the . match newlines.

<(\w+) name="([^"]*)">(.*?)</\1> 

After this regex, the first group contains the tag, the second the tag name, and the third the content between the tags. So replacement string could look like this:

[$1 name="$2"]$3[/$1] 

In C#, this looks like:

newString = Regex.Replace(oldString,      @"<(\w+) name=""([^""]*)"">(.*?)</\1>",      "[$1 name=\"$2\"]$3[/$1]",      RegexOptions.Singleline); 
like image 33
Andomar Avatar answered Sep 19 '22 21:09

Andomar