-2

I need a regex for fetching the value in the </span> tag

<span class="booking-id-value">U166097</span>

value required: U166097

can please someone suggest me. I have tried using

<span class="booking-id-value">(.+?)

but it is not deriving the desired result it display on "U"

Cœur
  • 37,241
  • 25
  • 195
  • 267
Richa Yadav
  • 49
  • 10

3 Answers3

1

I think you need to be more specific about your expected value - below I'll just accept alphabetic and numeric characters as value - and more flexible about your tag, then I can suggest you to use a regex like this:

/<\s*span.+?class\s*=\s*"\s*booking-id-value\s*".*?>/s*([A-Za-z0-9]+)\s*<\//

Regex Demo

shA.t
  • 16,580
  • 5
  • 54
  • 111
0

? after the .+ makes it ungreedy, tells it to match as little as possible - and that’s just the first U in this case.

Remove the ?, and instead look for the closing </span> after (.*) to terminate what is matched correctly:

<span class="booking-id-value">(.+)<\/span>

https://regex101.com/r/vt4pgN/1/

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • You should keep it ungreedy, otherwise it won't work on inputs with multiple closing spans, it will go straight from the first to the last. – Julian Zucker Sep 13 '17 at 12:32
  • @JulianZucker you’re right, did not take that into account. Would only be a problem if there where no line breaks in between though, unless you make the dot match line breaks as well. If we can assume that those spans in question don’t contain any line breaks _inside_, it will still work though. But you are right, depending on the exact requirements this might need further adaptation. – CBroe Sep 13 '17 at 12:36
0

Regex:

<span.*>(.*)<\/span>

Substitute with:

$1

Result

Bodz
  • 37
  • 8