469,945 Members | 2,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

how do I handle linebreaks in Regex?

I'm trying to do some regex in C# but for some reason linebreaks are causing
my regex to not work.

the test string goes like this:

string ss = "<tagname
something=45678&somethingelse=12345>blah</tagname>\r\n<tag2>stuff</tag2>";

and my regex code is like:

Regex pat = new Regex("something=([0-9]*).*>(.*)<.*<tag2>(.*)</tag2>",
RegexOptions.Multiline);

foreach (Match m in pat.Matches(ss)) {

foreach (Group g in m.Groups) {
Console.Write(g+", ");
}
Console.WriteLine();
}

but it never works unless I remove the "\r\n" from the test string.

how do I get around that? I thought that's what the RegexOptions.Multiline
was supposed to take care of?
Aug 22 '06 #1
7 2208

Oops, looks like I should have used "Singleline" option, not Multiline!
Aug 22 '06 #2
Ok there's definitely something I am not understanding with Regular
Expression...

when I switched to Singleline mode it worked great for that small test
string but in the main file all hell broke loose and my Console seems to be
printing out the entire file over and over in some infinite loop.

As another test I tried this:

Regex pat = new Regex("<a.*>(.*)</a>", RegexOptions.Singleline);

on a regular web page, and again same problem... it just starts spitting out
the whole file over and over. If I switch to Multiline mode it works great
but it does not pick up and <atags which is broken up by one or more
newlines... how do I get around this?
Aug 22 '06 #3
"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:EE**********************************@microsof t.com...
Ok there's definitely something I am not understanding with Regular
Expression...

when I switched to Singleline mode it worked great for that small test
string but in the main file all hell broke loose and my Console seems to
be
printing out the entire file over and over in some infinite loop.

As another test I tried this:

Regex pat = new Regex("<a.*>(.*)</a>", RegexOptions.Singleline);

on a regular web page, and again same problem... it just starts spitting
out
the whole file over and over. If I switch to Multiline mode it works great
but it does not pick up and <atags which is broken up by one or more
newlines... how do I get around this?
Instead of . use [\S\s]

-- Alan
Aug 22 '06 #4
Here's the problem:

<a.*>(.*)</a>

You're using the '.' character escape. This means "any character that is not
a newline character". When the MultiLine option is on, the '.' matches
newlines, and therefore matches everything. So, when you have MultiLine off,
the newline character sequence breaks the match. When you have it turned on,
everything is matched in the first match until the end of the last match.
Here:

<a
something=45678&somethingelse=12345>blah</a>
<a>stuff</a>

With MultiLine ON, the first tag matches, and so does every character after
it, until the last "</a>" in the string.

Using the '.' character escape is to Regular Expressions what using an
Atomic Bomb is to warfare. You want to be as specific as possible, rather
than the opposite.

In the example below, I use a very specific character class: [^<] - This
means any character that is NOT a '<' (or a '>' in another case). This way,
the match stops where the first '<' character is found, and the rest of the
match is evaluated from the remaining portion of the string. The following
will find the 2 (and only 2) matches in your example. Each matching value
will be in Group 1 of the match.

<a[^>]*>([^<]*)</a>

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:EE**********************************@microsof t.com...
Ok there's definitely something I am not understanding with Regular
Expression...

when I switched to Singleline mode it worked great for that small test
string but in the main file all hell broke loose and my Console seems to
be
printing out the entire file over and over in some infinite loop.

As another test I tried this:

Regex pat = new Regex("<a.*>(.*)</a>", RegexOptions.Singleline);

on a regular web page, and again same problem... it just starts spitting
out
the whole file over and over. If I switch to Multiline mode it works great
but it does not pick up and <atags which is broken up by one or more
newlines... how do I get around this?

Aug 22 '06 #5
Kevin, thanks for that tip, it works great for that example!

So there is no way in regex to say something like, accept all characters
until you hit a specific group of characters, like "</div>" ? Like let's say
you are scanning a web page for a specific opening <divtag, and you want to
grab all the text between that and the next closing </divtag, so the
contents may include many <'s and >'s inside. I guess regex is not the way to
go for doing something like that?
Aug 22 '06 #6
Hi Mr. Nobody,

Actually, Regex is quite capable of handling this sort of situation. In the
solution I gave you I went for the simplest solution necessary, as I
understood it. Your second example was using an image tag, which would not
contain other tags. In a case where other tags might be nested, you would
need to use a different set of Regex tools.

For example, to get all text between 2 matching beginning and ending tags,
when there are no nested tags, you would use something like:

<([^>]*)>([^<]*)</\1>

This indicates that a match begins with the left angle bracket. The left
angle bracket is followed by a sequence of any length of characters that are
NOT a right angle bracket. This sequence of characters is put into Group 1.
This is followed by a sequence of any length that is NOT a left angle
bracket, followed by a left angel bracket and a forward-slash. The last part
of the match is that the text from the first tag (Group 1) is matched,
followed by a right angle bracket.

For tags that contain nested tags, something like the following might work:

<(table|form|div)[^>]*>(.*?)</\1>

This indicates that tables, forms, and divs (I'm sure I may have missed one
or two) are matched. The ending tag uses the group captured from the first
tag. Group 2 contains the content.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:6A**********************************@microsof t.com...
Kevin, thanks for that tip, it works great for that example!

So there is no way in regex to say something like, accept all characters
until you hit a specific group of characters, like "</div>" ? Like let's
say
you are scanning a web page for a specific opening <divtag, and you want
to
grab all the text between that and the next closing </divtag, so the
contents may include many <'s and >'s inside. I guess regex is not the way
to
go for doing something like that?

Aug 23 '06 #7
Awesome!

Thanks Kevin, you were a BIG help- thanks to you I was able to do what I
needed without resorting to trying and parse the HTML response which would
have been a nightmare and been really slow compared to regex!

I needed to write a program which basically monitors forum responses by
stripping out key strings like the post itself, it's thread ID and the user's
name and then performing some checks on them.

Thanks again for your help
Aug 23 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Tomba | last post: by
5 posts views Thread by Mechphisto | last post: by
1 post views Thread by =?Utf-8?B?TXJOb2JvZHk=?= | last post: by
12 posts views Thread by Torsten Bronger | last post: by
5 posts views Thread by David Schwartz | last post: by
6 posts views Thread by K Viltersten | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.