How can I select the third group of numbers using Regex.
With the following string.
21|2|964|Texto 02
I want to select only 964.
I only managed to extract all the digit chunks with \d+ regex.
Thanks.
If you cannot split with | and get Item 3 from the resulting array, you may use
^(?:[^|]*\|){2}\K\d+
See the regex demo.
Alternatively, use
^(?:[^|]*\|){2}(\d+)
and grab Group 1 value. See another regex demo.
Details
^ - start of string
(?:[^|]*\|){2} - 2 sequences of:
[^|]* - any 0+ chars other than |
\| - a literal | symbol
\K - a match reset operator discarding the text matched so far
\d+ - 1 or more digits.
Related
I am trying to get the full words between two '|' characters
example string: {{person label|Jens Addle|border=red}}
here I would like to get the string: Jens Addle
I have attempted with the following:
(([A-Z]\w+))
However, this separates the result into two words and I would like to get it as a single entity.
This should put the value into $1.
Key is escaping the pipes, capturing what is in between and being non-greedy about it.
\|(.+?)\|
This should work in your case: /\|(.*?)\|/gm, or without the flags \|(.*?)\|.
This regex matches all character between two | characters. (\| - the | character, (.*?) - match everything and capture)
Here is the regex101 page.
You can use
\|\K[^|]*(?=\|)
(?<=\|)[^|]*(?=\|)
See the regex #1 demo and regex #2 demo.
Details:
(?<=\|) - a location that is immediately preceded with a | char
\|\K - matches a | char and then "forgets" it
[^|]* - zero or more chars other than a | char
(?=\|) - a location that is immediately followed with a | char.
Matching 1 ore more words between the pipe chars can be done using a capture group.
Note that [A-Z]\w+ matches at least 2 characters.
\|([A-Z]\w+(?: \w+)*)(?=\|)
\| Match |
( Capture group 1
[A-Z]\w+ Match an uppercase char A-Z and 1+ word characters
(?: \w+)* Optionally repeat matching a space and 1+ word characters
) Close group 1
(?=\|) Positive lookahead, assert | to the right
See a regex demo.
To take the format of the example string into account, you might also make the pattern a bit more specific:
{{[^|]*\|([A-Z]\w+(?: \w+)*)\|[^|]*}}
See another regex demo.
I have the following regular expressions that extract everything after first two alphabets
^[A-Za-z]{2})(\w+)($) $2
now I want to the extract nothing if the data doesn't start with alphabets.
Example:
AA123 -> 123
123 -> ""
Can this be accomplished by regex?
Introduce an alternative to match any one or more chars from start to end of string if your regex does not match:
^(?:([A-Za-z]{2})(\w+)|.+)$
See the regex demo. Details:
^ - start of string
(?: - start of a container non-capturing group:
([A-Za-z]{2})(\w+) - Group 1: two ASCII letters, Group 2: one or more word chars
| - or
.+ - one or more chars other than line break chars, as many as possible (use [\w\W]+ to match any chars including line break chars)
) - end of a container non-capturing group
$ - end of string.
Your pattern already captures 1 or more word characters after matching 2 uppercase chars. The $ does not have to be in a group, and this $2 should not be in the pattern.
^[A-Za-z]{2})(\w+)$
See a regex demo.
Another option could be a pattern with a conditional, capturing data in group 2 only if group 1 exist.
^([A-Z]{2})?(?(1)(\w+)|.+)$
^ Start of string
([A-Z]{2})? Capture 2 uppercase chars in optional group 1
(? Conditional
(1)(\w+) If we have group 1, capture 1+ word chars in group 2
| Or
.+ Match the whole line with at least 1 char to not match an empty string
) Close conditional
$ End of string
Regex demo
For a match only, you could use other variations Using \K like ^[A-Za-z]{2}\K\w+$ or with a lookbehind assertion (?<=^[A-Za-z]{2})\w+$
I am trying to find a regex expression to match strings with 4 repeating digits and optional hyphens in between, such as:
-3-3-3-3-
-1111-
2222-
0-0-00
Currently I have:
\-?(\d(\-*))\1{3}\-?
which matches the first two but not the last two. Any suggestions?
You may use
^-?(\d)(?:-?\1){3}-?$
See the regex demo. To find the pattern in a larger string, remove the ^ and $ anchors:
-?(\d)(?:-?\1){3}-?
If the pattern is a part of a longer pattern, you might have to adjust the backreference number (if there are other capturing groups in the pattern).
Details
^ - start of string
-? - an optional -
(\d) - Group 1: any digit
(?:-?\1){3} - three occurrences of an optional - and then the same value as captured in Group 1
-? - an optional hyphen
$ - end of the string.
This question already has answers here:
regex to match substring after nth occurence of pipe character
(3 answers)
Closed 4 years ago.
This is not a duplicate. I have checked before asking.
I have this string separated with | and I want to match the nth element.
aaaaaaaaa aaa|bb bbbbb|cccc ccccccc|ddd ddddddd|aaa aaa aaaaa|zzz zzz zzzzzzz
The closer I got is using this pattern but it buggy:
([^\|]*\|){2}[^\|]*
https://regex101.com/r/EYZbK5/1
This is plain pcre. In this context, javascript such .split() cannot be used.
Say I want to get the 3rd element cccc ccccccc what regex should I use?
You could use an anchor to assert the start of the line and then repeat not matching a | followed by a | 2 times. Then capture the third part in a capturing group which will contain cccc ccccccc
^(?:[^|]*\|){2}([^|]*)
Regex demo
Explanation
^ Assert the start of the line
(?: Start non capturing group
[^|]*\| Match not a | using a negated character class zero or more times followed by a |.
){2} close non capturing group and repeat that 2 times
([^|]*) Capture in a group matching not a | zero or more times
You may use
^(?:[^|]*\|){2}\K[^|]*
See the regex demo.
Details
^ - start of string
(?:[^|]*\|){2} - a non-capturing group matching two consecutive occurrences of
[^|]* - a negated character class matching 0+ chars other than |
\| - a | char
\K - match reset operator that discards the text matched so far
[^|]* - 0+ chars other than |.
To avoid empty string matches, you may replace the last [^|]* with [^|]+.
you may try this and take group2
(\|?(.*?)(?:\|)){3}
demo and explanation
I have a problem to set regular expression for multiple dates with comma separator.
I have dates like as :
2017-03-25, 2017-03-27, 2017-03-28
please help me guys.....
i am trying to set php validation for selecting multi dates (jquery calender).
my regex is :
$value = "2017-03-25, 2017-03-27, 2017-03-28";
preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])([0-9])*$/",$value)
You are matching only a single date with 0+ digits after it with your regex.
You may use the following fix:
^([0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01]))(?:,\s*(?1))*$
See the regex demo
Details:
^ - start of string
([0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])) - Group 1 matching and capturing a date-like substring
(?:,\s*(?1))* - zero or more sequences of:
, - comma
\s* - 0+ whitespaces (remove * to only match one, or use ? to match 1 or 0 whitespaces)
(?1) - recurse Group 1 subpattern
$ - end of string.