user266003
user266003

Reputation:

Can't capture a group in regex

I am trying to get the value of the attribute of value which can be any integer number including ones less than zero

val source = """...some noise .... <input type="hidden" name="myId" id="myId" value="1234"/> ...some noise ....""" // or value="-5678"/>

val regex = """<input type="hidden" name="myId" id="myId" value="([-?\\d+])"/>""".r
regex findAllIn source 

And I get scala.util.matching.Regex.MatchIterator = empty iterator

Upvotes: 0

Views: 72

Answers (3)

Boris the Spider
Boris the Spider

Reputation: 61128

Your regex doesn't do what you think it does. It matches - or ? or \ or d or +. You have put everything into a character class. You should use:

(-?\d++)

As you are using the Scala tripe quote you don't need to double escape \.

Upvotes: 1

Shadowlands
Shadowlands

Reputation: 15074

As well as what @M42 says (remove the square brackets), you want to remove one of the backslashes before the digit marker - remember that you are inside a "triple-quote" string - you don't need to escape backslashes:

(-?\d+)

Upvotes: 0

Toto
Toto

Reputation: 91373

Change this part:

([-?\\d+])

to:

(-?\\d+)

Upvotes: 0

Related Questions