0

I have a problem with regular expression. I have this code:

<div class="textPremium">Premium Access 25.04.2014 | 20:59<br>Server: ON | Bandwidth: 200MB<br /></div>

I want to match:

"25.04.2014" (date)

"20:59" (time)

"200" (bandwidth)

Here is my regular expression:

 <div class=\"textPremium\">Premium Access (.*) \| (.*)<br>Server: ON | Bandwidth: (.*)MB<br><\/div>

I matched the date and time, but I can't match bandwidth. Adding \ before | doesn't work.

Community
  • 1
  • 1
Adrian
  • 3
  • 1
  • 1
    It's because your string reads `Bandwidth: 200MB
    ` and you are matching `Bandwidth: (.*)MB
    ` (**note the `
    `**). This is why you shouldn't use regular expressions to match HTML (because HTML isn't a regular language).
    – Sam Apr 25 '14 at 16:53
  • Post your reflex, not just link. – Billy Moon Apr 25 '14 at 16:54
  • `
    Premium Access (.*) \| (.*)
    Server: ON \| Bandwidth: (.*)MB
    <\/div>`
    – devnull Apr 25 '14 at 16:55
  • Simple error. ~10 minutes wasted. Thank you @Sam – Adrian Apr 25 '14 at 16:57
  • 1
    No problem, you actually had tried to solve it, asked a clean question, and got a quick solution. Exactly what the community is for, welcome to SO and glad I could help. – Sam Apr 25 '14 at 17:06

1 Answers1

1

Try this updated expression:

<div class="textPremium">Premium Access (.*) \| (.*)<br\s*/?>Server: ON \| Bandwidth: (.*)MB<br\s*/?></div>

As you can see in my comment, you were trying to match <br> when the line break was formatted as <br />. HTML is not a regular language, and you should not use regex to attempt to match it.

However, I improved your expression to match both <br> and <br /> by using <br\s*/?>. This will allow 0+ characters of whitespace followed by an optional /. Also note, that I changed the delimiters from / to ~ so that we didn't need to escape ever /.

Community
  • 1
  • 1
Sam
  • 20,096
  • 2
  • 45
  • 71