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.
Related
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
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 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
I am trying to replace " on inch i.e. 12" wall would become 12 inch wall
I have 2 patterns working:
/\b([0-9]+)"/ -> preg_replace('/\b([0-9]+)"/', '$1 inch ', $string)
and
/\b([0-9]*)"/ -> preg_replace('/\b([0-9]*)"/', '$1 inch ', $string)
what is a difference between them then, why + and * works same way here ?
cheers,
/Marcin
The + means find the previous character/group 1 or more times.
The * means find the previous character/group any amount of times (0-infinity)
/\b([0-9]+)"/ requires that there is at least one digit between the word boundary and the ", whereas /\b([0-9]*)"/ also accepts zero digits. So the first does not match a space followed by " and the second does.
If you want to mach both new 15 " tv and new 15" tv you need to match against a space character that may or may not be present:
/\b([0-9]+)\s?"/
This matches a word boundary, followed by a sequence (on or more) numbers, optionally followed by one space (or tab), followed by a ". I presume that's what you are looking for.
If not, you should first define strings that must match and strings that may not match.
Quick regex question (since i am horrible at it)
I have a field that can only have either:
XXXXXXXXXX or XXXXXX-XXXX where X is a real number.
Bonus if the regex works well with PHP's regex functions.
The Answer:
Here's the code from RoBorg's answer, for those interested.
if(!preg_match("/^\d{6}-?\d{4}$/", $var))
{
// The entry didn't match
}
/^\d{6}-?\d{4}$/
That's
^ Start of string
\d{6} a digit, repeated exactly 6 times
-? an optional "-"
\d{4} a digit, repeated exactly 4 times
$ end of string
Just quickly, it'd be something like this:
\d{6}-?\d{4}
You may have to escape the hyphen in PHP.
If you want a specific number (the question wording was originally somewhat ambiguous), search for (e.g.):
^123456-?7890$
If searching for any 10 digit number with that format, search for:
^\d{6}-?\d{4}$
The ? qualifier after the dash means "0 or 1 occurrences of the preceding entity"