Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
XML
<br/>Partly Cloudy/Win-
d</div>


Using the above two-line string from a web page, why wont this regex string work,
\<\w*\>\w*\s\w*\/\w*\-\r\n\w*\<\/\w*\>, in finding the above string?


The carriage return is not working (\r\n)

It works fine in Expresso but not in VB 2010 pro.

Thnx a bunch in advance for the correct answer.
rspercy60
Posted

It won't work this way. To match end-of-line, use '$'. It also depends on option: it ether means end of string or end of any line, see your regex options. The '$' match is much better than character-based match, because it is system-independent: there are few different ways to delimiter lines.

—SA
 
Share this answer
 
Comments
rspercy65 22-Mar-11 18:38pm    
The dollar sign is not working in expresso or Rad Software Regex Designer or VB 10. I used it with
RegexOptions.Multiline. The'$' doesnt seem to work in any of these programs.
Sergey Alexandrovich Kryukov 22-Mar-11 18:44pm    
If must be some mistake. Not all features work in all cases, but '$' is a very old and basic one.
I used it everywhere (many different languages and libraries), $ always worked.

I'm sure the problem is different. Did you pay attention to options? The option can be named "multiline" or something...

--SA
rspercy65 22-Mar-11 20:08pm    
Using my line above....How would I use it, with the single quotes, braces, brackets...what?
Sergey Alexandrovich Kryukov 22-Mar-11 20:11pm    
Sorry, I only paid attention of end-of-line so far...
Could you kindly describe informally what should you match with examples?
--SA
Sergey Alexandrovich Kryukov 22-Mar-11 20:13pm    
Also, when you say it doesn't work in VS, what library do you mean? Or IDE?
If you need to process two-line sample, what other samples if would match, and what not?
--SA
Hi
Try this way..

C#
string test=@"bla bla <br/>Partly Cloudy/Win-
            d</div> bla bla";




C#
string pattern2 = "<[\\w]*/>{1}[\\w\\s/]*-{1}\\r\\n[\\w\\s/]*</[\\w]*>";
 Regex reg2 = new Regex(pattern2);
 Match match2 = reg.Match(test, 0);
 MessageBox.Show(reg2.IsMatch(test).ToString());
 MessageBox.Show(match2.ToString());




This is as per your example. But inside the html tags better use [a-zA-Z] because inside html tags only text is allowed.

I suggest take out the carriage return part. Why because it is not guarantee the carriage return occurs at the same place every time.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 22-Mar-11 22:40pm    
Seems reasonable, a 5 (taking into account OP did not specify what's needed).
I think '-' and carriage return are redundant: why and HTML itself is wrong: 1) there is not "win- d" (carriage return is the same as blank space), 2) today windy, tomorrow rainy -- it is not a criteria. None of that is your fault.
--SA
Albin Abel 22-Mar-11 23:34pm    
It is summer here now. Thanks SAKryukov
Sergey Alexandrovich Kryukov 23-Mar-11 14:01pm    
Not _here_. I had to wipe snow on the day you post your good Answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900