I have a specific string followed by number (which changes). I want a regex function for that.
The string is Pay $3.14
The regex I am using is:
"^(Pay|[0-9]*)$"
But it is not giving me the result.
Can someone please guide?
Use
\bPay\s*\$(\d+(?:\.\d+)?)
See proof. The number is in capturing group 1.
EXPLANATION
NODE EXPLANATION
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
Pay 'Pay'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\$ '$'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
) end of \1
I looked at another question of yours [https://stackoverflow.com/questions/63125938/finding-an-element-using-regex-selenium-java], and I think this is what you want:
https://regex101.com/r/OI9Tcq/1
If $ is not included in the text, then you can use:
https://regex101.com/r/B8i4xe/1
Both expressions will grab just the numbers, as per this:
"Pay 13.61" I want to save the element 13.61 in a string and compare with 0.
Original Answer
https://regex101.com/r/OI9Tcq/1
In the event that this was more in line with what you want, I've attached my original answer.
^(Pay\\s+\\d+\\.?\\d+)$^(Pay\s+\$\d+(\.\d+)?)$^[^\$]*