user231185
user231185

Reputation: 1

regex can't recognize "\n"?

so at the end the end(after few days of debuging) i found a problem. It isnt in regex at all :/ . It seams that i was trimming ekstra white spaces with

intput= Regex.Replace(input, "\\s+", " ");

so all new lines are replaced with " ". Stupid! Moderator, please remove this if unnecesary!

I have regexp for tokenizing some text and it looks like this :

"(?<html>Ç)|
(?<number>\\d+(?:[.]\\d+)?(?=[][ \f\n\r\t\v!?.,():;\"'„Ç]|$))|
(?<other>(?:[^][Ç \f\n\r\t\v!?.,():;\"'„A-Za-zčćšđžČĆŠĐŽäöÖü][^ Ç\f\n\r\t\vA-Za-zčćšđžČĆŠĐŽäöÖü]*)?[^][ Ç\f\n\r\t\v!?.,():;\"'„A-Za-zčćšđžČĆŠĐŽäöÖü](?=[][!?.,():;\"'„]*(?:$|[ Ç\f\n\r\t\v])))|
(?<word>(?:[^][ Ç\f\n\r\t\v!?.,():;\"'„][^ Ç\f\n\r\t\v]*)?[^][ Ç\f\n\r\t\v!?.,():;\"'„])|
(?<punctuation>[][ \f\n\r\t\v!?.,():;\"'„])"

Problem is in this part: (?<punctuation>[][ \f\n\r\t\v!?.,():;\"'„]). So when im prsing text with input "\n\n" it is grouping in punctuation matches: " "," " - in other words, space and space... and I don't know why?

Upvotes: 0

Views: 387

Answers (3)

martin
martin

Reputation: 2997

If you put an @ in front of string you can use single backslashes and line-breaks will be recognized.

 @"(?<html>Ç)|

Upvotes: 2

leppie
leppie

Reputation: 117220

Set RegexOptions.IgnorePatternWhiteSpace

Update:

Are you sure [^] is correct? Unless it's somekind of character group (that I have never used), that will be the same as . . Same goes for []. Perhaps I just have not used all of RE before :p

Upvotes: 0

Bobby
Bobby

Reputation: 11576

I could be wrong, but you need to hand the String as String to the RegEx...means you need to escape the backslashes.

... (?=[][ \\f\\n\\r\\t\\v!?.,():;\\" ...

Or otherwise C# will replace \n with a linebreak within the RegEx-Statement.

Edit: It's also possible to use literal strings, but the need to be marked with beginning @ (see Martin's answer).

Upvotes: 5

Related Questions