开发者

regex can't recognize "\n"?

开发者 https://www.devze.com 2022-12-14 10:48 出处:网络
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

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!?.,():;\"'„开发者_如何学JAVA][^ Ç\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?


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).


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

 @"(?<html>Ç)|


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

0

精彩评论

暂无评论...
验证码 换一张
取 消