Click here to Skip to main content
14,979,508 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
   
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
rspercy65 22-Mar-11 20:15pm
   
match the two-lines below(with tags) exactly the same.

<br/>Partly Cloudy/Win-
d</div>
Sergey Alexandrovich Kryukov 22-Mar-11 20:22pm
   
It it is "exactly the same" search exact substring. Purpose of regex is different.
Your regex attempt also covers a big set of matches: w* means several words, without any detail on what letters are, etc.
--SA
Sergey Alexandrovich Kryukov 22-Mar-11 20:19pm
   
I would suggest use something like this:

\<br\/\>w*\/w*/<

So far, I don't know your goal exactly though, just to give an idea...

--SA
rspercy65 22-Mar-11 20:27pm
   
What I need to see is this...
<br/>Partly Cloudy/Win-d</div>

Then I use the Replace statement and remove the(<br/> and </div>) tags and then place the results in a Label in a weather application
Sergey Alexandrovich Kryukov 22-Mar-11 20:41pm
   
I'm telling you, it should be some variable string. What?!
--SA
rspercy65 22-Mar-11 21:55pm
   
<br/>Partly Cloudy/Win-
d</div>

We use this string above as the data were going to find.
Using the above two-line string from a web page, we use this regex string,
\<\w*\>\w*\s\w*\/\w*\-\r\n\w*\<\/\w*\>. It works in expresso and Regex Designer and
this is the result '<br/>Partly Cloudy/Win-d</div>'. But, this does not work in Visual Basic 2010 pro. What I am trying to do is get this result so I can use it in a weather five day forecast.
Sergey Alexandrovich Kryukov 22-Mar-11 22:52pm
   
No, if it is forecast, which seems obvious, it's not "Win-d" every day. You should select definitive criteria for full set of forecasts. Reference the Web site, most likely I'll find out.
--SA
rspercy65 23-Mar-11 4:50am
   
There is 13 different types, Cloudy, Cloudy/Wind, Partly Cloudy, Rain/Wind/Sleet,
Partly Cloudy/Win-
d...and the list goes on

The html tags are all the same(<br/> and </div>). There is 5 different forecast that I want to retreive. Here is my code
Dim patternAA As String = "<[\w]*/>{1}[\w\s/]*-{1}\r\n[\w\s/]*|\<\w*\/\>\w*\<\/\w*\>" &
"|\<\w*\/\>\w*\s\w*\<\/\w*\>|\<\w*\/\>\w*\s\w*\-\w*\<\/\w*\>|\<\w*\/\>\w*\s\w*\<\w*\/\>" &
"|\<\w*\/\>\w*\s\w*\<\/\w*\>|\<\w*\/\>\w*\s\w*\/\w*\s\w*\<\/\w*\>|\<\w*\/\>\w*\s\w*\s\w*\s\w*\<\/\w*\>" &
"|\<\w*\/\>\w*\s\w*\s\w*\<\/\w*\|\<\w*\/\>\w*\s\w*\/\w*\<\/\w*\>|\<\w*\/\>\w*\/\w*\/\w*\<\/\w*\>"
'Get the five conditions and the Images that we need.
For Each m In Regex.Matches(myTB.Text, patternAA, RegexOptions.Multiline)
strMatch += m.Value
Next
strMatch = strMatch.Replace("<br/>", "")
strMatch = strMatch.Replace("</div>", ",")
' MessageBox.Show(strMatch)
parts3 = Split(strMatch, ",")

lblCon1.Text = parts3(0)
RetrieveForecastA()
lblCon2.Text = parts3(1)
RetrieveForecastB()
lblCon3.Text = parts3(2)
RetrieveForecastC()
lblCon4.Text = parts3(3)
RetrieveForecastD()
lblCon5.Text = parts3(4)
RetrieveForecastE()

This works as long as there is no "carriage return" in the string i need from web page.
Albin Abel 22-Mar-11 22:35pm
   
You are right "It it is "exactly the same" search exact substring. Purpose of regex is different". OPs pattern is a more generalized one and may result in many matches. My 5
Sergey Alexandrovich Kryukov 22-Mar-11 22:37pm
   
Isn't this obvious. I still think this is OP's communication problem, and it is not "exactly the same".
Thank you, Albin.
--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.
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month



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