Regex filter only values in real

1

I have the following regex:

preg_match_all('/([0-9]+[\.]*[0-9]*[\,.]*[0-9]*)/', $string, $matches)

If I get a string:

  

1 - João da Silva number 123456 with the value of R $: 6,298.65

I have a return:

array (size=27)
  0 => string '1' (length=1)
  1 => string '123456' (length=5)
  2 => string '6.298,65' (length=8)

However, I would like to return only the actual value: 6,298.65

    
asked by anonymous 16.03.2018 / 13:50

2 answers

3

The problem is that you are leaving everything as "optional" ( * ). The REGEX that can help you is:

~R\$:[\t ]*(\d{1,3}\.?)+(,\d{2})?~

See REGEX101

    
16.03.2018 / 13:56
3

I think you're looking for the default numero.numero,numero , I'd make a regex like this:

/\d+\.\d+,\d+/
  • \d is a shortHand that looks for numbers, is the same as the set [0-9]
  • + is a quantifier that searches for 1 or more elements, is the same as {1,}
  • \. will search for a string because I used \ .

and Running on regex101

If you need the text to start with you R$: you should add R\$: at the beginning of the regex, you must use Scape\ because $ is a border that looks at the end of the text. >     

17.03.2018 / 17:01