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
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
Using the following regex doesn't work to validate /command number.
Here's what format of numbers I need to "validate"
/command 1
/command 0.5
/command 0.12345678
As you may see the value needs to be positive and the decimals are maximal 8.
I've done some research and found:
\/command\s?[\S]
But this work only for /command 1.
\/command\s?[\S] here [\S] will match only one non-space character so you can use and nothing else.
/command 1 // 1 : match one non-space
/command 0.5 // 0.5 :more than one non-space character so won't match
/command 0.123456789 // won't match
\/command\s?\S(\.\S{1,8})?
(\.\S+)? : ? match zero or one
(\.\S{1,8}) match . and 1 - 8 non-space character
more specifically for digits use
To define max 8 digit limit , use \d{1,8}
^\/command\s?\d(\.\d{1,8})?$
Note : if you want to match more digits before . e.g /command 123.5 then use
^\/command\s?\d+(\.\d{1,8})?$
as suggested by #jen and #serge
When you want to validate an entire string the first thing to remember is to enclose your pattern between the start and end of the string anchors: \A...\z
About the number with 8 decimals max there's nothing particular to say except that if you don't want a trailing dot you need to use an optional group and the correct quantifier: \d+(?:\.\d{1,8})?
Note also that you are free to change the pattern delimiter with an other character. This way you don't have to escape the slash that isn't a special regex character.
Result:
$pattern = '~\A/command \d+(?:\.\d{1,8})?\z~';
(feel free to make the space optional if needed)
This is because you missed '+' in [\S]+
But the above will not distinguish between numbers and other symbols.
To pick on 'numbers', you can use something like the following in 'perl'-like regex:
/\/command\s*[0-9.]+/
I want to check if phone no contains this pattern AABBCC
Where A[0-9],B[0-9],C[0,9] They should be different e.g 112233,553322,887766
Let Us Suppose
I Have a phone no 03334112233
It will say yes pattern matched.
PHP Code but It Is For Exact String
$str = 'aabbaabbccaass'; //or whatever
if (preg_match('/(?!.*?aabbcc)^.*$/', $str))
echo "accepted\n";
else
echo "rejected\n";
Problem i don't know how to do if string is for numbers
Possible Duplicate
but it does not contain answer and exact detail.
Edited :
I want to match the last 6 characters of the string in this pattern AABBCC e.g 03329112233
To match number with AABBCC format, you can use this pattern:
(?:(\d)\1(?!\1)){2}(\d)\2
example of use:
if (preg_match('/(?:(\d)\1(?!\1)){2}(\d)\2/', $str)
echo "rejected\n";
else
echo "accepted\n";
But if you have other tests to do (for example that there is only digits), it can be more flexible to use it in this way:
if (preg_match('/(?!.*(?:(\d)\1(?!\1)){2}(\d)\2)^\d+$/', $str)
echo "accepted\n";
else
echo "rejected\n";
pattern details:
(?: # open a non capturing group that describes a repeated digit
(\d) # capture the first digit with group 1
\1 # a backreference to group 1 (the same digit thus)
(?!\1) # check with a negative lookahead that the same digit doesn't follow
){2} # repeat the group two times
(\d)\2 # same thing for digits 5 & 6 (the lookahead isn't needed here)
Note that the digit in the capture group change at each repetition of the non capturing group (because the negative lookahead forces it).
Notice: if you want to reject numbers that contains, for example, 111122 or 112222 or 111111, you only need to remove the negative lookahead.
if you want to reject numbers with the format 112211 or 448844, you must change the pattern like this: (\d)\1(?!\d{0,2}\1)(\d)\2(?!\2)(\d)\3
As I understand, you only want to match the last 6 characters of the string, if they are digits, and of 3 all different digit pairs. Would also use a lookahead and some pattern like this:
(?>((\d)\2)(?!.*\1)){3}$
\2 checks for an equivalent of 2nd capturing group, which is one digit (shorthand \d)
using a negative lookahead to check, if not followed by .* any amount of any characters, followed by equivalent of 1st capturing group (which contains 2 equal digits).
{3} 3 repitions at $ end of string.
Test on regex101.com, Regex FAQ
Your regex should be like this:
^((\d)\2){3}$
It is simpler and also works.
You can use capturing groups and backreferences like this:
if (preg_match('/(?!.*(.)\1(.)\2(.)\3)^.*$/', $str))
The (.) will match any single character and assign it to a group. The first instance is assigned to group 1, the second to group 2 and so on. Later in the pattern, the backreference \1 will match exactly what was previously captured in group the first group, \2 will match what was captured in the second group, etc.
You probably will also want to use \d to match any single digit (it's only necessary to use this outside of the lookahead) and a {n,m} quantifier to match between n and m digits. For example, the following will match any sequence of 7 to 10 digits that does not contain a subsequence like AABBCC:
if (preg_match('/(?!.*(.)\1(.)\2(.)\3)^\d{7,10}$/', $str))
I am using this RegEx:
\s*((\w*\.*\s*)+[.*(\w)]+(?=\d{4}))(\d{4}\s*\,)*
And the goal is to match words with the last one that ends with a dot, follower with 4 digits ending with a comma.
This is a test.2014,
And it works fine. Now, I would like to add the possibility to have a whitespace (\s) between the "test." and "2014,", but the whitespace is not a mandatory parameter, it should be matched if there.
Could you please help me on how to add that to my regex? How can we set not mandatory parameters ?
Thank you.
Try this pattern
\s*((\w*\.*\s*)+[.*(\w)]+\s*(?=\d{4}))(\d{4}\s*\,)*
\s* there is a zero or more time space. I added it before date pattern
Or try:
\s*((\w*\.*\s*)+[.*(\w)]+(?=\s*\d{4}))(\s*\d{4}\s*\,)*
There are a couple ways you can do this. You can use the question mark, an asterisk, or you can specify the range of viable occurrences as is shown in the table below.
Regular Expressions Quantifiers
* 0 or more
+ 1 or more
? 0 or 1
{3} Exactly 3
{3,} 3 or more
{3,5} 3, 4 or 5
\s*((\w*\.*\s*)+[.*(\w)]+(\s+)(?=\d{4}))(\d{4}\s*\,)*
\s*((\w*\.*\s*)+[.*(\w)]+(\s*)(?=\d{4}))(\d{4}\s*\,)*
\s*((\w*\.*\s*)+[.*(\w)]+\s{0,1}(?=\d{4}))(\d{4}\s*\,)*
And the goal is to match words with the last one that ends with a dot,
follower with 4 digits ending with a comma
(?:\s*\w+)*\s*\w+\.\d{4},
Now, I would like to add the possibility to have a whitespace (\s)
between the "test." and "2014,", but the whitespace is not a mandatory
parameter,
(?:\s*\w+)*\s*\w+\.\s*\d{4},
Try this
/\s*((\w*\.*\s*)+[.*(\w)]+(\s*)(?=\d{4}))(\d{4}\s*\,)*/
I have a large string (multiple lines) I need to find numbers in with regex. The position the number I need is always proceeded/follow by an exact order of characters so I can use non-capturing matches to pinpoint the exact number I need. I put together a regex to get this number but it refuses to work and I can't figure it out!
Below is a small bit of php code that I can't get to work showing the basic format of what i need
$sTestData = 'lak sjdhfklsjaf<?kjnsdfh461uihrfkjsn+%5Bmlknsadlfjncas dlk';
$sNumberStripRE = '/.*?(?:sjdhfklsjaf<\\?kjnsdfh)(\\d+)(?:uihrfkjsn\\+%5Bmlknsadlfjncas).*?/gim';
if (preg_match_all($sNumberStripRE, $sTestData, $aMatches))
{
var_dump($aMatches);
}
the number I need is 461 and the characters before/after the spaces on either side of this number are always the same
any help getting the above regex working would be great!
This link RegExr: My Reg Ex (to an online regex genereator and my regex) shows that it should work!
g is an invalid modifier, drop it.
Ideone Link
With regard to that link, which regular expression engine is it working from? Built in Flex, so probably the ActionScript RegExp engine. They are not all the same, each one varies.
You have a number of double-backslashes, they should probably be single in those strings.
$sTestData = 'lak sjdhfklsjaf<?kjnsdfh461uihrfkjsn+%5Bmlknsadlfjncas dlk';
$lDelim = ' sjdhfklsjaf<?kjnsdfh';
$rDelim = 'uihrfkjsn+%5Bmlknsadlfjncas ';
$start = strpos($sTestData, $lDelim) + strlen($lDelim);
$length = strpos($sTestData, $rDelim) - $start;
$number = substr($sTestData, $start, $length);
Using regex you can accomplish your goal with the following code:
$string='lak sjdhfklsjaf<?kjnsdfh461uihrfkjsn+%5Bmlknsadlfjncas dlk';
if (preg_match('/(sjdhfklsjaf<\?kjnsdfh)(\d+)(uihrfkjsn\+%5Bmlknsadlfjncas)/', $string, $num_array)) {
$aMatches = $num_array[2];
} else {
$aMatches = "";
}
echo $aMatches;
Explanation:
I declared a variable entitled $string and made it equal to the variable you initially presented. You indicated that the characters on either side of the numeric value of interest were always the same. I assigned the numerical value of interest to $aMatches by setting $aMatches equal to back reference 2. Using the parentheses in regex you will get 3 matches: backreference 1 which will contain the characters before the number, backreference 2 which will contain the numbers that you want, and backreference 3 which is the stuff after the number. I assigned $num_array as the variable name for those backreferences and the [2] indicates that it is the second backreference. So, $num_array[1] would contain the match in backreference 1 and $num_array[3] would contain the match in backreference 3.
Here is the explanation of my regular expression:
Match the regular expression below and capture its match into backreference number 1 «(sjdhfklsjaf<\?kjnsdfh)»
Match the characters “sjdhfklsjaf<” literally «sjdhfklsjaf<»
Match the character “?” literally «\?»
Match the characters “kjnsdfh” literally «kjnsdfh»
Match the regular expression below and capture its match into backreference number 2 «(\d+)»
Match a single digit 0..9 «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below and capture its match into backreference number 3 «(uihrfkjsn+%5Bmlknsadlfjncas)»
Match the characters “uihrfkjsn” literally «uihrfkjsn»
Match the character “+” literally «+»
Match the characters “%5Bmlknsadlfjncas” literally «%5Bmlknsadlfjncas»
Hope this helps and best of luck to you.
Steve