I'm a beginner to regex, so I apologize in advance if this is a naive question!
I have a string with two values separated by a comma: 12.345678,23.45678901
I am trying to use regex (this is a requirement) to return the first value with 3 decimals 12.345 and the second value with 2 decimals 23.45.
Ideally, the full regex match would be 12.345,23.45
I am able to get the first value 12.345 using the following regex: ^\d+\.\d{0,3}.
This works well because it only returns the full match (there is no Group 1 match). But I'm pretty stumped on how to get the second value 23.45 to be returned in the same string.
I've also tried this regex:
(^.{0,6})(?:.*)(,)(.{0,5}), which correctly parses the first and second values, but the full match is being returned with too many decimals.
Full match: 12.345678,23.45
Group 1: 12.345
Group 2: ,
Group 3: 23.45
Any suggestions are welcome! Thank you in advance.
You can use this regex to get your data:
^(\d+\.\d{3})\d*,(\d+\.\d{2})\d*$
It looks for digits followed by . and 3 decimal places (first capture group), then some number of digits followed by a comma (discarded) and then digits followed by a . and 2 decimal places (second capture group), followed finally by some number of digits and the end of string (discarded).
To use in PHP
$str = '12.345678,23.45678901';
preg_match('/^(\d+\.\d{3})\d*,(\d+\.\d{2})\d*$/', $str, $matches);
echo "first number: {$matches[1]}\nsecond number: {$matches[2]}\n";
Output:
first number: 12.345
second number: 23.45
Demo on 3v4l.org
If you need to get both matches in the $matches[0] array (using preg_match_all), you can use this regex:
(?<=^)\d+\.\d{3}(?=\d*,)|(?<=,)\d+\.\d{2}(?=\d*$)
This regex looks for either
the start of string followed by some digits, a . and 3 digits (followed by some number of digits and a comma); or
a comma, some number of digits, a . and 2 digits (followed by some number of digits and the end of string).
To avoid capturing the unwanted data it is checked for using positive lookaheads and lookbehinds.
Demo on 3v4l.org
Related
I try to validate a pattern like 123*5000.
The pattern has 3 parts.
First, the [123] must contain three digits unique number.
The second is *, and the third is 5000, must be an integer, not unique is fine.
If the pattern is 223*443 it will return false (first part 223, number 2 it not unique).
If the pattern is 908*22 it will return true (first part contain unique number, second part is *, third part is integer).
If the pattern is 34*5000 it will return false (first part only contain 2 digits), and etc.
I try regex \^[0-9][0-9][0-9][*][0-9]+$\ but it did not solve the first part pattern that must contains unique number.
Can anyone help me?
You might write the pattern as:
^(?!\d*(\d)\d*\1)\d{3}\*\d+$
The pattern matches:
^ Start of string
(?!\d*(\d)\d*\1) Negative lookahead, assert not 2 of the same digits
\d{3}\*\d+ Match 3 digits, then * and 1 or more digits
$ End of string
See a regex demo.
i have the text:
<a href="blahblahblah-dynamic" class="blahblahblah-dynamic"
title="blahblahblah-dynamic">2.550,00 €</a>1000 € 900 € 5000 € ......
and the expression:
#(\d+[\.\,]\d*?)\s?[€]#su
that matches:
550,00
example in: regexr
How can I match the whole:
2.550,00 ?
p.s I dont want to match the others 1000, 900 and numbers without , and/or .
In other words, I want to match d,d or d.d,d
so the question possible duplicate, does not cover my case.
Can someone help me on this?
You might use:
([0-9]{1,3}(?:.[0-9]{3})*\,[0-9]+)\s?€
This will match in a capturing group 1-3 digits. Then repeats in a group a dot and 3 digits and at the end will match a comma followed by one or more digits.
After the capturing group \s?[€] is matches but is not part of the group.
If you want to match exactly 2 digits after the comma you could update ,[0-9]+ to ,[0-9]{2}
As an alternative you could match your value without a capturing group and use a positive lookahead (?=\s?[€]) to assert that what is on the right side is an optional whitespace character followed by €
[0-9]{1,3}(?:.[0-9]{3})*\,[0-9]+(?=\s?€)
i'm guessing you got that value in a variable or something , why not try get it with
$value = substr($dataReceivedFromA, 0, -1); // returns "2.550,00 "
i really think there is no point in using regex here if you only wanna get rid of the sign
I have looked everywhere but I am stuck and need some help.
I believe I need a regex to replace a quote " (for inches) at the end of a number.
For example 21.5" with 21.5inch
I need to do it only if the " has a number before it.
Thanks in advance!
The regex for this task is
(\d)"
Here is DEMO with explanation
Try this:
(?<=\d)"
https://regex101.com/r/lC9tZ7/2
It should grab the " as long as it follows a digit.
If it can have spaces between the digit and ", try this:
(?<=\d)\s*"
https://regex101.com/r/cT1bF7/3
Try (\d+\.{0,1}\d+)\s*"
Explanation: Lets try matching 21.54 inch
\d+ matches 21
\.{0,1} escapes decimal notation and matches if there's a . atleast 0 times (i.e., there is no decimal at all) and atmost 1 times (i.e., a number can only have at most 1 decimal). So, we have so far matched 21.
\d+ matches the remaining 54. So far matched 21.54
\s* forgives if there is any space followed by the number
" finally ensures that the number is followed by the inch notation.
Check this demo link here.
In PHP how can I determine if a zip code exists within a string, and if so, return the zip code. In this context, a zip code can be either a string of 5 numbers (ie "55543") or a string of 5 numbers connected to 4 more numbers with a hyphen (ie "74657-9993")..
Can anyone help me out with a Regex pattern I can use with preg_match or any other good ways of doing this?
I have preg_match_all("/\d{5}/", $str, $matches); so far, but that doesn't account for the possible second 4 digits or the hyphen.
5 number connected to 4 more numbers with a hyphen:
preg_match_all("/\b\d{5}(?:-\d{4})?\b/", $str, $matches);
(?:-\d{4,})? is an optional group, with a hyphen, and at least 4 digits after it.
Edit: Forgot to prevent longer than 5 digits for the first part (and 4 digits for the second part), using a word boundary.
EDIT2:
Okay, something else I just noticed is that if you have 12345-12345 but don't want to get any number form this, you would use:
preg_match_all("/\b\d{5}(?!-\d{1,3}\b|-\d{5})(?:-\d{4})?\b/", $str, $matches);
The negative lookahead prevents the match of -12345 (or more digits, or less than 4 digits) if present, but allow only 4 digits ahead.
regex101 demo
Your pattern is
\b\d{5}(?:-\d{4})?\b
See it here on Regexr.
An important part here are the word boundaries \b, they ensure that not a part of a number is matched.
\d{5} is matching 5 digits as you already had it
(?:-\d{4})? is the optional part (because of the ? after the the group). The ?: at the start of the group is just making the group non-capturing.
I have recently implemented this in javascript
/^(\s*|\d{5}([\-]\d{4})?)$/;
Just modify your regex to allow the optional prefix:
preg_match_all("/\d{5}(\-\d{4})?/", $str, $matches);
I would like to detect whether a user-chosen pin contains 4 identical numbers e.g 1111 or 2222. I'm using preg_match in PHP.
How can I adapt this answer to do this?
You could use this regex:
/(\d)\1{3}/
This matches a single digit (\d), and then matches that same digit 3 times \1{3}.
count(array_unique(str_split($pin))) > 1
Adapting from the answer you link to:
\b(\d)\1{3}\b
Instead of using \1+ that would match any number of repetitions of the first digit, you substitute it with \1{3} that will only allow three repetitions of the first digit, thus giving you the desired four digits when matched.
Or if you prefer:
\b(\d)\1\1\1\b