Grabbing number next to a dollar sign with optional thousands and decimals - php

I am trying to grab a number that can be in the format $5,000.23 as well as say, $22.43 or $3,000
Here's my regular expression, this is in PHP.
preg_match('/\$([0-9]+)([\.,]*)?([0-9]*)?([\.])?([0-9]*)?/', $blah, $blah2);
It seems to match numbers in the format $5,500.23 perfectly fine, however it doesn't seem to match any other numbers well, like $0.
How do I make everything optional? Shouldn't grouping () and using a question mark do that?

This should do the trick:
\$[\d,.]*[\d]
Debuggex Demo
Specific PHP Example:
$re = "/\\$[\\d,.]*[\\d]/";
$str = "\$1 klsjdfgsjdfg \$100 kjdfhglsjdfg \$1,000 jljsdfg \$1,000.00 ldfjhsdf";
preg_match_all($re, $str, $matches);
Regex 101 Demo

Related

Breaking apart a string in PHP into three parts

I have a serial number string I need to break apart into 3 parts.
The serial numbers look like this:
FOOB123456AB
BAR789123BC
First part: A-Z letters of variable length
Middle part: 6 digit numerical string
Last part: 2 digit letters
How can I break this apart using PHP so I can work with each individual part?
Regular expression can help here. See preg_match().
Try:
$regex = "/^([a-z]*)([\d]{6})(.*)$/i";
$serial = "FOOB123456AB";
$result = preg_match($regex, $serial, $matches);
// result in $matches[1], $matches[2], $matches[3]
This assumes one serial number per string. If you don't have that, text is easy to break up with explode() or similar, and then iterate over the resulting array.

PHP Regex to get text between 2 words with numbers

i'm trying to get the string between two words in a entire string:
Ex.:
My string:
...'Total a Facturar 123,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado'...
I'm using
/(?<=Total a Facturar )(.*?) Recepcionado/
I need the highlighted characters (26,860161,16080,580310,760)
and i get 221,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado with my pattern.
The numbers of the string are always different, i need the numbers that are together without a space.
Thanks
EDIT:
Here is the entire string: eval.in/802292
I hope this will be helpful
Regex demo or Regex demo 2
Regex: (?:\d+(?:\,\d+){2,})
For above question you can also use it like this (?:\d+(?:\,\d+){4})
1. (?:\d+) this will match digits one or more.
2. (?:\,\d+){2,} Adding this in expression will match patterns like , and digits {2,} for 2 or more than 2 times.
PHP code: Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = "Total a Facturar 123,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado";
preg_match("#(?:\d+(?:\,\d+){2,})#", $string, $matches);
print_r($matches);

Regex to find digits between the last set of parentheses

i was struggling to make a regex which extract the digits bettween the last set of parentheses from a string, and this is what i came with until now:
^.*?\([^\d]*(\d+)[^\d]*\).*$
E.g.:
This is, (123456) a string (78910);
It returns 123456 , which is great but i need it to look at the last set and return 78910.Also , i want the regex to ignore everything but digits:
This is, (123bleah456) a string (789da10);
Should return: 78910
UPDATE
Using regex:
(\d+)(?!.*\d)
For string:
Telefon Mobil (123)Apple iPhone 6 128GB Gold(1567)asd234
Will return 234 when it should be 1567
Rubular
You can extract the last one by use of greed:
.*\(\K\d+
See demo at regex101
\K resets beginning of the reported match.
For your more specific updated case slightly modifiy the regex and strip out non-digits.
$str = "This is, (123bleah456) a string (789da10);";
if(preg_match('/.*\(\K\d[^)]*/', $str, $out))
$res = preg_replace('/\D+/', "", $out[0]);
See demo at eval.in

Group regex with fix part

$txt = "toto1 555.4545.555.999.7465.432.674";
$rgx = "/([\w]+)\s([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/";
preg_match($rgx, $txt, $res);
var_dump($res);
I would like to simplify this pattern by avoiding repeating "([0-9]+)" because i don't know how many they are.
Any one can say me how ?
Here is a direct answer to the question, as you have stated it:
/[\w]+\s[0-9]+(?:\.[0-9]+)+/
However, note that I have removed all of the numbered capture groups. This could be problematic, depending on what you're actually trying to achieve.
It is not possible to "count" with capture groups in regular expressions, so you would need to write some other code (i.e. not just one match, with one regex, and using back-references) to deal with this if you wish to run any queries like "What digits appear after the fifth "."?"
There are two ways you can do this. If you just need to verify that the string matches the pattern, this regex will do the job: \w+\s(?:[0-9]+\.?)+
However, if you need to split the string in to it's component parts (in my interpretation, the beginning word followed by the sequence of decimal separated numbers), then you could use this pattern: (\w+)\s((?:[0-9]+\.?)+)
The second pattern will return the beginning word, toto1 in group 1, followed by the decimal separated numbers in group 2 555.4545.555.999.7465.432.674 which you could then split in PHP if required: $sequence = explode('.', $matches[2]);
What you need can be obtained with a preg_split with a regex matching 1 or more whitespaces or dots:
$txt = "toto1 555.4545.555.999.7465.432.674";
$rgx = '/[\s.]+/';
$res = preg_split($rgx, $txt);
print_r($res);
See the PHP demo
If you need a regex approach, you can use a \G based regex with preg_match_all:
'~(?|([\w]+)|(?!\A)\G[\s.]*([0-9]+))~'
See the regex demo and a PHP demo:
$txt = "toto1 555.4545.555.999.7465.432.674";
$rgx = '~(?|(\w+)|(?!\A)\G[\s.]*([0-9]+))~';
preg_match_all($rgx, $txt, $res);
print_r($res[1]);
Pattern details:
The (?|...) is a branch reset group to reset group IDs in all the branches
(\w+) - Group 1 matches 1+ word chars
| - or (then goes Branch 2)
(?!\A)\G - the end of the previous successful match
[\s.]* - zero or more whitespaces or dots
([0-9]+) - Group 1 (again!) matching 1 or more digits.

Grab Number using Preg_Match

$str = 'title="room 5 stars"';
preg_match_all('/title="([0-9]+)"/sm', $str, $rate);
I need to grab number 5 from title. The regex doesn't work!
If i do this:
preg_match_all('/title="([0-9]+)"/sm', $str, $rate);
I get:
room 5 stars
However, this one doesn't return anything:
'/title="([0-9]+)"/sm'
Where did i go wrong?
You're not taking into account the words around the number, try this:
$str = 'title="room 5 stars"';
preg_match_all('/title=".*(\d+).*"/', $str, $rate);
// The number is then in $rate[1][0];
You forgot to match the text before and after your number.
Try with : /title=".*([0-9]+).*"/
PS: you don't need m and s option
* is a greedy match, it might give wrong results sometimes.
You can use /title=".*?(\d+).*?"/ which is a lazy match and will search the least characters.
You can also try this free tool for regex matching: RegExr

Categories