I'd like to match these strings using preg_match, basicaly only repeated patterns of digits and a comma (optional), no letters
123
123,
123,456
123,456,
123,456,789
123,456,789,
etc...
but not
abc
123,abc
123,abc,456
abc,123,456
thanks
Put comma and the pattern to match one or more digits inside a non-capturing group and then make it to repaet zero or more times. And also don't forget to add an optional comma at the last.
^[0-9]+(?:,[0-9]+)*,?$
DEMO
Related
In a monetary pattern I look for thousand separator
(?:[. ]\d{3})*
In this case the thousand separator could either be . or . But how to make sure that the pattern will not match patterns where the thousand separator is mixed?
Only match patterns like
.123.123.123
123 123 123
Do not match
.123 123 123.123
You can match the first separator and then use a backreference to match the same separator for the remaining digit groups:
^(?:([. ])\d{3}(\1\d{3})*)?$
Demo: https://regex101.com/r/u4zPuf/1
If you don't want to match empty strings:
^([. ])\d{3}(?:\1\d{3})*$
In parts the pattern matches:
^ Start of string
([. ]) Capture group 1, match either . or a space
\d{3} Match 3 digits
(?:\1\d{3})* Optionally repeat a back reference to what is captured in group 1 followed by 3 digits
$ End of strings
Regex demo
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'm trying to construct a regular expression that would match a pattern as such:
word1, word2, word3
So basically I want ", " to appear twice and to have words between them. So far I came up with:
$general_content_check = preg_match("/^.*, .*$/", $general_content);
But this matches only ", " several times in a string.
Can someone help me with this please?
It depends what you mean by "word" but you can start by trying this:
^[^,]+(?:, +[^,]+){2}$
Explanation:
^ Start of line/string.
[^,]+ A "word" (anything that isn't a comma - including whitespace, etc.)
(?: Start non-capturing group
, + A comma then any number of spaces
[^,]+ A word
) Close group
{2} Repeat group exactly two times
$ End of line/string.
Other possible definitions of "word":
Anything except whitespace or comma: [^\s,]+
Only letters in A-Z: [A-Z]+ (optionally add case-insensitive flag)
Any letter in Unicode in any language: \p{L}+ (not widely supported)
Etc...
Try
"/^\w+, \w+, \w+$/"
i need a regex for: 123,456,789,123,4444,... basically comma separated values. The INT part can be 1-4 numbers long, followed by a comma...always in this form...
/^([0-9]{1,4})(\,)?$/
This obviously doesn't work...
Thanks!
Try this:
/^\d{1,4}(?:,\d{1,4})*+$/D
This will match any comma-separated sequence of one or more digit sequences with one to four digits. The D modifier makes sure that any trailing newline character does not mistakenly result in a positive match.
Try this:
/^[0-9]{1,4}(?:,[0-9]{1,4})*$/
This will match any comma separated sequence of one or more digit sequences with one to four digits. (?:…) is a so called non-capturing group that’s match cannot be referenced separately like you can with “normal” capturing groups (…).