1

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?

4
  • ^(Pay\\s+\\d+\\.?\\d+)$ Commented Jul 28, 2020 at 18:40
  • ^(Pay\s+\$\d+(\.\d+)?)$ Commented Jul 28, 2020 at 18:47
  • What is your question? Do you wish to confirm the string contains a digit? Do you wish to extract each sequence of digits? Must the string begin with "Pay"? Must the string contain the representation of a dollar amount? Commented Jul 28, 2020 at 21:12
  • you can use this one, ^[^\$]* Commented Jul 29, 2020 at 19:27

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.