I have these two regular expression
^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$
^(9){1}[0-9]{9}+$
How can I combine these phrases together?
valid phone :
just start with : 0098 , +98 , 98 , 09 and 9
sample :
00989151855454
+989151855454
989151855454
09151855454
9151855454
You haven't provided what passes and what doesn't, but I think this will work if I understand correctly...
/^\+?0{0,2}98?/
Live demo
^ Matches the start of the string
\+? Matches 0 or 1 plus symbols (the backslash is to escape)
0{0,2} Matches between 0 and 2 (0, 1, and 2) of the 0 character
9 Matches a literal 9
8? Matches 0 or 1 of the literal 8 characters
Looking at your second regex, it looks like you want to make the first part ((98)|(\+98)|(0098)|0) in your first regex optional. Just make it optional by putting ? after it and it will allow the numbers allowed by second regex. Change this,
^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$
to,
^(?:98|\+98|0098|0)?9[0-9]{9}$
^ this makes the non-grouping pattern optional which contains various alternations you want to allow.
I've made few more corrections in the regex. Use of {1} is redundant as that's the default behavior of a character, with or without it. and you don't need to unnecessarily group regex unless you need the groups. And I've removed the outer most parenthesis and + after it as that is not needed.
Demo
This regex
^(?:98|\+98|0098|0)?9[0-9]{9}$
matches
00989151855454
+989151855454
989151855454
09151855454
9151855454
Demo: https://regex101.com/r/VFc4pK/1/
However note that you are requiring to have a 9 as first digit after the country code or 0.
Related
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Regular expressions: Ensuring b doesn't come between a and c
(4 answers)
Closed 3 years ago.
I know this sounds easy but I am stuck.
I want to match strings that has asterisk *.
Essentially I want to allow strings having asterisk at front/back/both but not middle:
(At max there will be 2 asterisks, front and both but no middle, and the presence string is a must)
ALLOW:
*string* *string string* string
DENY:
*str*ing*
*str*ing str*ing* str*ing
*string*****
I tried
^\\*?((?!\\*).)*\\\*?$
and somehow it works.
Can someone explains how this works?
And verify if this is correct because regex..hard to debug and check..
You can use the following regex:
^\*?\w+\*?$
demo: https://regex101.com/r/vwuXv2/1/
Explanations:
^ anchor imposing the start of a line
\*? a * appearing at most one time
\w+ at least 1 word char appearing in the text ([a-zA-Z0-9_] feel free to change it depending on your need)
\*? a * appearing at most one time
$ end of line anchor
Now if you are interested in partial line matches, you can use the following regex:
(?<=^| )\*?\w+\*?(?=$| )
demo: https://regex101.com/r/vwuXv2/2/
Explanations: you add lookbehind, lookahead assertions.
Adding Japanese characters as requested in the comment (add in [^*\s] all the characters you need to exclude from the words):
^\*?[^*\s]+\*?$
demo: https://regex101.com/r/RaCmwt/1/
or
^\*?[[:alpha:]]+\*?$
(with unicode flag enabled) or just
^\*?\p{L}+\*?$
demo: https://regex101.com/r/RaCmwt/2/
You can simply say: Optionally start with asterisk, 0 or more arbitrary characters except asterisk, optionally end with asterisk.
^\*?[^*]*\*?$
https://regex101.com/r/bibCEc/2
An alternative is to inverse the match and test if there is not ( i.e. if(!...)) any asterisk not at the begin or end using negative look behind and look ahead:
(?<!^)\*(?!$)
https://regex101.com/r/8St0M4/2
According to your recent edit you would use the quatifier + to match 1 or more characters:
^\*?[^*]+\*?$
https://regex101.com/r/bibCEc/3
Hi I tired to use RegEx in PHP. The following elements I like to get with it:
<a="300">
<a="300"b="300">
<b="300">
The Problem is that I get only
<a="300">
<b="300">
with the following RegEx:
<(a|b)="[0-9]*">
What do I have to change, that I get all three elements? Is there a ANDOR operator?
Assuming your problem is rather a simple string processing than serious parsing, I would modify your regex like this:
<(a|b)="[0-9]+".*>
I added .* to allow characters inbetween " and >.
or a slightly my-flavored version:
<[ab]="\d+"[^>]*?>
piping single characters with | are less favored over [...]
\d is for series of digits
[^>]*? for characters other than >
You need an additional grouping, to specify, that you would accept multiple of that kind:
echo '<a="300">
<a="300"b="300">
<b="300">' | egrep '<((a|b)="[0-9]*")+>'
<a="300">
<a="300"b="300">
<b="300">
Regex is not boolean logic. The | symbol in regex is not an OR operator; it is referred to as alternation, which works similarly but is not quite the same thing. If you're just trying to match one of multiple characters, you should use square brackets [] to create a character set. In this case, [ab] matches a or b, just as [0-9] matches 0 or 1 or 2 etc.
Here's the pattern that I would suggest
<[ab]="\d+"(?:[ab]="\d+")?>
There is something really I couldn't understand is how can I check my previous match with the next character and set starting and ending character please guys help me.
Here is an Example of my string
..A..B..A...B.A.B
What I'm trying to do is starting of string:
1=> Check the first character is .. or A
2=> and the Second thing is String cannot be like this ..A..A it must be like ..A..B.. and sequence.
3=> Ending character must be .. or B and won't be A
However, I can match the first character like so ^([A]{1}|[.]{1,100}) But when I'm trying this same way with ending character it is not working and I'm not getting how to do the step 2.
Save my day guys. Thanks
Failed Regex: ^[\.{1,40}|A{1}]+(?!A)+(B)+(?!B)+(B|\.{1,40})$
This regex should match the description you've given:
^(?:\.+?)?(A\.+?B\.?|\.\.)+$
^ is the start of the string (or line if m modifier is used).
(?:\.+?)? is one or more ., but it optional.
A\.+B\.? is looking for an A any amount of .s then a B and an optional ..
| is an alternative pattern we'll look at
\.\. are 2 .s
+ allows for the whole group to occur once or more
$ is the end of the string (or line, again depends on modifier being used)
Demo: https://regex101.com/r/OUJxxc/3/ (Probably with a clearer description than I provided)
Data:
N15319542045C13_1_3/61488007C13-130083_1_3/61488007C13-130083-1_1_3/P1197443641_1_3SD|1
NP1196939393_1_3SU|OD=2/7;|BNP1196939393_1_3SU|OD=2/7;|BNP1196930222_1_3SU|OD=4/11;|
NP1196930222_1_3SU|OD=4/11;|
N15319384625C13_1_3/61445794C13-130077_1_3SD||BN15319384625C13_1_3/61445794C13-130077_1_3SD||
RegExp:
(N(.*?)S([UID])\|(.*?))(?:B|\|.?$)
I am trying to find 7 matches using above regex but only 6 are matching. Not sure how to fix to match 1st line as well.
Format:
N(key)S(action)|(value or end)
end depend on different matches
I solved it if someone else needs:
(\x15(.*?)\x01([UID])\|(.*?))(?:.*?\x08|.*\|?$)
The regex didn't work because after the S[UID] you expect 2 | as per the regex but in the first input string there is only one.
One fix is to make the second group optional and move out the string end anchor $
(N(.*?)S([UID])\|(.*?))(?:B|\|.?)?$
Regex Demo
Or may be more simpler as
N.*?S[UID]\|.*$
Regex Demo
I'm using the following PCRE expression with preg_match to check if the value I want is a digit or not.
(?P<id>[\d]+)
It works, but now I want it to match the same conditions except if the whole content equals 0 (zero).
Example result
1 valid
10 valid
0 invalid
Expression context
#^(?P<controller>.*?|home)(?:/(?P<action>.*?|index)(?:/(?P<id>[\d]+))?)?$#uD
Can you try this?
#^(?P<controller>home)(?:/(?P<action>index)(?:/(?P<id>[1-9][\d]*))?)?$#uD
In your example, just using the digits, it's matching first, so obviously 0 gets captured. Assuming your match string is more complex than that, in which case the "1-9 once and 0-9 0 or more times" should do it for you.
See: http://regex101.com/r/yK6mR5
You can use this regex:
^(?!0+$)(?P<id>\d+)$
Online Demo: http://regex101.com/r/dB2eT3
UPDATE:
Working regex:
'#^(?P<controller>[^/]*|home)(?:/(?P<action>[^/]*|index)(?:/(?!0+$)(?P<id>\d+))?)?$#'