Regular expression to pick up the value after the comma

Asked

Viewed 5,650 times

6

With the following expression (,)([0-9]*) manage to catch the ,25 but would like to catch only the 25.

decimal (10,25)

In this case how can I disregard the ,

  • Consider informing the programming language that you are using Iago, as regex implementations may vary between languages.

3 answers

8

If what you want is simply to leave only the number in a capture group just take the comma out of the parentheses:

,([0-9]*)

To ensure you have any number you can use the + instead of * (can use the \d instead of [0-9] if you want):

,(\d+)
  • friend, thank you so much for the help, of the options I chose yours as the best alternative.

  • Okay, @Iagoleão! Consider marking the answer as accepted then.

5

Use the following regular expression that will work:

(?<=,)([0-9]+)

This regular expression uses lookbehind that nothing else is to find the values of the defined group ([0-9]+) if before he finds the comma (?<=,).

A complete explanation about lookahead and lookbehind can be found on this website.

Follow a code exeplo with Python:

import re
m = re.search('(?<=,)([0-9]+)', '(0,25)')
print m.group(0) #25
  • 2

    I don’t know what language OP is using, but I would like to warn him that this answer, as far as I know, does not work in Javascript (for example) for lack of lookbehinds support.

-3

Just use (,25),takes all occurrences of ,25.

\w,(25*) to the full number.

Test in Regexr

  • it wants to return only the numbers after the comma, not the comma and only the number 25

  • 1

    True... I’ll keep the answer so you can see how it’s not done.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.