I am having trouble crafting a regex. For example, in the string A123 4HEL5P6 789 I want to match all the numbers 4, 5, 6, 7, 8, 9 but not 1, 2, 3.
I have tried using negative look behind with the regex (?<!^\w)\d+ but this matches the numbers in the first word.
Edit: Any numbers in the first continuous sequence of characters should not be matched, the first continuous sequence being from start (^) to a whitespace (\s). In 09B8A HE1LP only 1 should be matched, not 0, 9, or 8, as these digits are in the first word.
If your dialect supports variable-length negative lookbehinds, then this should work:
r = /(?<!^\w*)\d/g
console.log(...'A123 4HEL5P6 789'.match(r))
Otherwise, you could use /^\w*|\d/g and discard the first match.
Related
I am trying to write a regex that can match the following instructions
A sequence of character with the “AT” prefix, followed by “nG” where n is a digit from 1 through 5 and then "G" and lastly followed by a suffix of 5 numeric digits.
Note: just the ordinary regular expression not language specific.
An example of a matching string is this: “AT1G01040”
Here is what I could construct AT[1-5]G(d\{1,5}) but I am not sure if it is the correct answer.
Please, I need your hand on this thanks.
If the number of digits at the end may be from 1 to 5, you may use
^AT[1-5]G[0-9]{1,5}$
See the regex demo.
Note that if the number of digits at the end must be exactly 5, you must remove 1,:
^AT[1-5]G[0-9]{5}$
Details
^ - start of string
AT - a sequence of chars AT
[1-5] - 1, 2, 3, 4 or 5
G - a G char
[0-9]{1,5} - any 1 to 5 consecutive occurrences of an ASCII digit (or - if you use {5} - exactly 5 occurrences)
$ - end of string.
I need regex that contains and have mandatory at least one of each
Uppercase letters -- A, B, C...
Lowercase letters -- a, b, c...
Numbers -- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9...
Special characters -- ` ~ ! # # $ % ^ & *
like aaaaAAA123! or 0987ZZZZZz#
This site surely has similar questions to validate passwords.
But it's still amusing to craft a regex for a particlar case.
The regex below will match strings that contain all 4 character groups.
^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[`~!##$%^&*])[a-zA-Z0-9`~!##$%^&*]+$
The 4 lookaheads garantee that at least 1 character for each character group is available.
I already have a regex to match only single digits in a comma-delimited string. I need to update it to match the strings like following:
5|5,4,3
2|1,2 , 3
The constraints are
it should start with a single digit in range of 1-5, followed by a pipe character (|)
the string followed by the pipe character - it should be a single digit in range of 1-7, optionally followed by a comma. This pattern can be repetitive. For e.g. following strings are considered to be valid, after the pipe character:
"6"
"1,7"
"1,2,3, 4,6"
"1, 4,5,7"
However following strings are considered to be invalid
"8"
"8, 9,10"
I tried with following (a other variations)
\A[1-5]\|[1-7](?=(,|[1-7]))*
but it doesn't work as expected. For e.g. for sample string
5|5,4, 3, 10,5
it just matches
5|5
I need to capture the digit before pipe character and all the matching digits followed by the pipe character. For e.g. in following sample string 5|5,4, 3, 2, 1 the regex should capture
5
[5, 4, 3, 2, 1]
Note: I am using Ruby 2.2.1
Also do you mind letting me know what mistake I made in my regex pattern which was not making it work as expected?
Thanks.
You could try the below regex.
^([1-5])\|([1-7]\s*(?:,\s*[1-7])*)$
Example:
> "5|5,4, 3, 2, 1".scan(/^([1-5])\|([1-7]\s*(?:,\s*[1-7])*)$/)
=> [["5", "5,4, 3, 2, 1"]]
OR
> "5|5,4, 3, 2, 1".scan(/([1-5])\|([1-7] ?(?:, ?[1-7])*)$/)
=> [["5", "5,4, 3, 2, 1"]]
You can try the following regex that will match digits and a group of comma/space separated digits after a pipe:
^[1-5]\|(?:(?:[1-7]\s*,\s*)+\s*[1-7]?|[1-7])\b
Here is a demo.
How to express a string that contains at most one pair of consecutive 1's in UNIX regex?
Some examples to accepted strings: 0, 1, 11, 12, 22, 11221212, 22112121, 23456 etc.
Not accepted ones: 111, 11311, 311311 etc.
This should work:
^([^1]+|1[^1])*(11)?([^1]|$)([^1]+|1[^1]|1$)*$
See it on regex101.
Explanation:
([^1]+|1[^1])*
This will match anything that doesn't contain consecutive 1, by matching either anything that doesn't contain a 1 or a 1 followed by something else.
(11)?([^1]|$)
This next part will match two consecutive 1 (if present) followed by either a different char or the end of the string. So it will match a pair of 1 not followed by another 1.
([^1]+|1[^1]|1$)*
The final part is very similar to the first one, except it will allow the string to end with a single 1.
This pattern would be much simpler if you could use a richer regex dialect (like the Perl dialect). In the standard unix regexes, you can't use lookaround expressions. Here's a Perl pattern:
^(?!.*111)(?!.*11.*11).*$
I'm still learning regex and was hoping someone could tell me what this regex does exactly. Thank you.
\d{8,9}0101\d{3}
Breaking it apart:
\d{8,9}
That means either eight or nine digits (0-9).
0101
That means the literal string 0101
\d{3}
That means precisely three number digits.
You can use Expresso to Know more.
Youre regex means
1.Any digit of 8 or 9 repetation
2 then 0101
3 then any digit of exact 3 repetation
I would recommend to start from some source where theory could be found, later on using some tools where you can interactively check how this knowledge can be applied.
http://www.regular-expressions.info/posix.html <- This site contains information about POSIX standard for regular expressions.
Personally for testing matching I use rubular.com, but it references ruby's implementation of regexps. So it also depends on what regexp implementation you use.
In your case it is simple to answer and there should be no difference between different regexp implementations, though.
(A) \d{8,9} - a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) repeated minimum 8 to maximum 9 times
(B) 0101 - literal string 0101
(C) \d{3} - 3 then any digit of exact 3 repetation
regex does = A + B + C
This finds 8 or 9 digits (numbers 0-9), followed by 0101 followed by exactly three digits...
(You should have been able to figure that out by searching!)
Autopsy:
\d{8,9} - a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) repeated 8 to 9 times
0101 - a literal string of the characters 0101
\d{3} - a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) repeated exactly 3 times
Note: repeated doesn't mean "the same character" but anything in the match. That means that "repeated exactly 3 times" for \d could be 111, 123, 989 etc.