waleed.mirza
waleed.mirza

Reputation: 85

Regex for find digits and character but at start have $ in string

I have written a regex:

\s*\$\d+\,?\.?\d*\,?\d*[mkb]?\s*

This regex fetches strings like $100, $1k, $1m or $1b.

It works fine but now I need it to return $1mm if there is $1mm in the string, not just $1m that is a part of $1mm.

Upvotes: 1

Views: 71

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626952

You may use

'~\s*\$\d[,.\d]*(?:mm|MM|[mkbMKB]?)\b\s*~'

See the regex demo.

Pattern details

  • \s* - 0+ whitespaces
  • \$ - a $ symbol
  • \d - 1 digit
  • [,.\d]* - 0+ digits, , or . chars
  • (?:mm|MM|[mkbMKB]?) - either mm, MM, or case insensitively k, m or b, or empty string
  • \b - a word boundary
  • \s* - 0+ whitespaces.

Upvotes: 1

Related Questions