2

I got this Regex problem where I need to select the last part of a string however no matter what I do it will only select on till it hit's a "\n" (newline) Here's what I have tried so far

sms = Regex.Match(datalog, "\"\\r\\n.*").ToString();

Or

sms = Regex.Match(datalog, @"\""\r\n.*").ToString();

Here is what datalog contains

"\r\n+CMT: \"+4528835769\",,\"13/09/11,15:09:32+08\"\r\nLinje 1\nLinje2\nLinje 3\r\n

And what i need the Rexex to return

"\r\nLinje 1\nLinje2\nLinje 3\r\n

but this is what it returns (and what the string sms ends up containing)

"\"\r\nLinje 1"

What am I doing wrong?? since it stops selecting after Linje 1\n I assume the problem is with the newline.

I have also tried using .+ instead of .* but the result is the same

EDIT: I found that it was as simple as:

sms = Regex.Match(datalog, @"\""\r\n.*", RegexOptions.Singleline).ToString();

4 Answers 4

1

period (.) does not match new lines by default. You need to enable this behavior by specifying RegexOptions.SingleLine.

The dot matches a single character, without caring what that character is. The only exception are newline characters. In all regex flavors discussed in this tutorial, the dot will not match a newline character by default. So by default, the dot is short for the negated character class [^\n] (UNIX regex flavors) or [^\r\n] (Windows regex flavors).

...

All regex flavors discussed here have an option to make the dot match all characters, including newlines. In RegexBuddy, EditPad Pro or PowerGREP, you simply tick the checkbox labeled "dot matches newline".

...

When using the regex classes of the .NET framework, you activate this mode by specifying > RegexOptions.Singleline, such as in Regex.Match("string", "regex", RegexOptions.Singleline).

Source: http://www.regular-expressions.info/dot.html

Sign up to request clarification or add additional context in comments.

Comments

1

Change your method call to...

sms = Regex.Match(datalog, @"\""\r\n.*", RegexOptions.MultilineMode);

Multiline mode ignores newlines, carriage returns etc.

More details at MSDN - Multiline Mode

1 Comment

Multiline mode does not affect dot/period.
0

There is an option in the Regex to use multiline mode. Here is an example :

http://www.dotnetperls.com/regexoptions-multiline

4 Comments

Multiline mode does not affect dot/period, only $ and ^
Look at the MSDN example from toadflakz. That's very close to what is required in the question. Just needs a bit tweaking.
"By default, $ matches only the end of the input string. If you specify the RegexOptions.Multiline option, it matches either the newline character (\n) or the end of the input string. It does not, however, match the carriage return/line feed character combination. To successfully match them, use the subexpression \r?$ instead of just $. "
Or, use singleline mode and leave the pattern unchanged ;)
0

You can change your regex line to

sms = Regex.Match(datalog, "(?<=\"\\r\\n).*", RegexOptions.Singleline).ToString();

The (?<=...) excludes that group from the reported match, so that it starts with "Linje 1". If you do need that, you can revert to your original regex.

The RegexOptions.Singleline changes the . to match also newlines/returns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.