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
Related
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.
I'm try to use preg_match to parse the line. But, couldn't get the result correctly.
I want to parse those lines with optional // char.
Basic idea is some lines may contain //0016OIXXXXXXX (//4 digit and 9 str) and some line may not contain (//4 digit and 9 str).
Next line \n is mandatory.
Here are some sample lines;
Line 1
1812121212DD2220,31NTRFNONREF
502?102330
Line 2
1811091109CD20693,12NTRFRMSOIC110871941//0016RFXXXXXXX
206?000801
Here is my patten;
/(\d{6})((\d{2})(\d{2}))?(C|D)([A-Z]?)([0-9,]{1,15})([A-Z]{1,4})([A-Z a-z 0-9]{1,16})(\/\/)([0-9A-Z]{1,16})(\s*\n)([0-9]{1,3})(\?)([0-9]{0,6})/
Current patten can only work with //. If without //, patten is broken and can't parse the line.
I don't know how should I add (\/\/)([0-9A-Z]{1,16}) as optional patten.
Thanks in advance
:)
Just make the optional part well... optional:
\d{6}(?:\d{4})?[CD][A-Z]?[0-9,]{1,15}[A-Z]{1,4}[A-Za-z0-9 ]{1,16}(?:\/\/[0-9A-Z]{1,16})?\s*\n[0-9]{1,3}\?[0-9]{0,6}
// ^^^^^^^^^^^^^^^^^^^^^^^
I've removed the capture groups for lisibility, if you really want to keep them:
(\d{6})((\d{2})(\d{2}))?(C|D)([A-Z]?)([0-9,]{1,15})([A-Z]{1,4})([A-Za-z0-9 ]{1,16})(?:(\/\/)([0-9A-Z]{1,16}))?(\s*\n)([0-9]{1,3})(\?)([0-9]{0,6})
DEMO
If Am I understanding right your question.
Please, try to use or expression between the groups and validate any you case.
For example
`(pattern1) | (pattern-2) | ( (pattern3) | (pattern 4) )`
Please, try to consider using the | expression.
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)
I need some help building a regex for get the value of XXX in the following set of possible matches:
+58XXXYYYYYYY
+580XXXYYYYYYY
0XXXYYYYYYY
XXXYYYYYYY
This are phone numbers so XXX is dynamic and will not hold always the same value. The RegEx is intended to be used on PHP so I know I should use preg_match() function but I have not idea about the regex. Can any give me some advice on this?
This sounds like it matches your requirements:
(\d{3})\d{7}$
With a Live Demo
If you want to match the last 3 numbers, you could use /([0-9]{3,3})$/.
The parenthesis indicates a capturing group (what you are looking for). Inside that group you want to match a pattern [...]of any number 0-9 exactly 3 times {3,3}. And finally you want to match the last occurrence of this pattern, so the $ indicates the end of the line.
A very handy tool to building simple regex queries is Debuggex. I use it all the time!
I have the following string that I need to match only the last seven digets between [] brackets. The string looks like this
[15211Z: 2012-09-12] ([5202900])
I only need to match 5202900 in the string contained between ([]), a similar number could appear anywhere in the string so something like this won't work (\d{7})
I also tried the following regex
([[0-9]{1,7}])
but this includes the [] in the string?
If you just want the 7 digits, not the brackets, but want to make sure that the digits are surrounded with brackets:
(?<=\[)\d{7}(?=\])
FYI: This is called a positive lookahead and positive lookbehind.
Good source on the topic: http://www.regular-expressions.info/lookaround.html
Try matching \(\[(\d{7})\]\), so you match this whole regular expression, then you take group 1, the one between unescaped parentheses. You can replace {7} with a '*' for zero or more, + for 1 or more or a precise range like you already showed in your question.
You can try to use
\[(\d{1,7})\]
If first pattern looks like yours (not only digits), then this should work for you to extract group of digits surrounded by brackets like ([123]):
\(\[(\d+)\]\)
From your details, lookbehind and lookaround seems to be good one. You can also use this one:
(\d{7})\]\)$
Since the pattern of seven digit is expected at the end of the line, engine need to work less in order to find the match.
Hope it helps!
Here is a benchmark (in Perl, but I think is close the same in php) that compares lookaround approach and capture group:
use Benchmark qw(:all);
my $str = q/[15211Z: 2012-09-12] ([5202900])/;
my $count = -3;
cmpthese($count, {
'lookaround' => sub {
$str =~ /(?<=\[)\d{7}(?=\])/;
},
'capture group' => sub {
$str =~ /\[(\d{7})\]/;
},
});
result:
Rate lookaround capture group
lookaround 274914/s -- -70%
capture group 931043/s 239% --
As we can see, capture is more than 3 times faster than lookaround.