Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
I have the following regex that I will use in Javascript eventually:
^(0|[1-9][0-9]*)([,;|\t]|(?:x) | {2,})
Given this sample text, I would like to get on the capture group 1 the quantity and on the capture group 2 the separator (either a comma, semicolon, pipe, tab, two or more spaces or a "x" followed by a single space):
2,Apples
3x Trees
2 Oranges
2x A3252893
15553x Moons
15553x Apples Test test
Regex101 example
Now, the question is about me wanting to match the single space as separator only if it is preceded by the "x" character, i.e. basically leaving out the "x" from the group. If I change the regex above by replacing the non-capturing group with the positive lookbehind, it doesn't work:
^(0|[1-9][0-9]*)([,;|\t]|(?<=x) | {2,})
Regex101 updated example
Why doesn't it match? Is that because it is used inside an alternation? And how can I fix that?
EDIT: OK, so positive lookbehind is not the correct solution because I didn't match any x before that. So how can I match the separators that I've listed above on the same numbered capturing group 2 without including the x but only the space (i.e. capture the space only if its preceded by an x while keeping the other variants as they are)?
The problem is that the look behind assertion for an "x" comes at a moment that the digits have been matched, so the previous character will always be a digit, not "x".
You can solve this by reading the "x" first (when it is there), before continuing. So insert x?
^(0|[1-9]\d*)x?([,;|\t]|(?<=x) | {2,})
On regex101
If you don't want to allow the "x" to be there when another delimiter (or two spaces) is used, then use a negative look behind assertion in those cases:
^(0|[1-9]\d*)x?((?<!x)[,;|\t]|(?<=x) |(?<!x) {2,})
On regex101
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I am trying to figure how I can search for a pattern that gives me only digits or digits followed by only one letter. I know I can use /\D\g to find only digits but I dont know how to find digits with only one letter after it. The letters can only be the following:['a','A','b','B','c','C','d','D','n','N','e','E','s','S','w','W']
const testPattern = /[A-Za-z][0-9]/
console.log('item_10a_object10a'.pattern(testPattern))
First, you need to group the whole thing with (), and end with /g so it matches multiple groups.
If you want digits first then a number, you need to put the letter block after the numbers: ([0-9][A-Za-z])
If you want multiple digits to match, you need a + after the numbers block: [0-9]+
All together: /([0-9]+[A-Za-z])/g
For reference, \d does the same thing as [0-9], so you could do /(\d+[A-Za-z])/g instead
LPT: use regex101 to build and test regex queries
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to implement regex validation for passport number.
Length: 5-20 char Alphanumeric
Allowed characters: a-z, A-Z, 0-9 (case insensitive)
Cannot contain only 1 digit or character, repeated
Example: (what I expect to see)
Z1234aZ - false // "Z" is repeated
aVBNa1 - false // "a" is repeated
ZXCVB123 - true
12zv - false // length 4
My regex: /^(.){2,}[A-Za-z0-9]{5,20}$/ but it doesn't work
How can i match that none of symbols doesn't repeat?
I'm thinking maybe you can use:
^(?:([A-Z\d])(?!.*\1)){5,20}$
See an online demo. Using case-insensitive matching it would mean:
^ - Start line anchor.
(?: - Open non-capture group:
([A-Z\d]) - Capture single character from class.
(?!.*\1) - Negative lookahead to prevent matched character to occur again using a backreference to 1st capture group.
){5,20} - Close non-capture group and match 5-20 times.
$ - End line anchor.
Not sure if I read it correctly, but if you require at least two digits and at least to alpha-chars then maybe add two positive lookaheads:
^(?=.*\d.*\d)(?=.*[A-Z].*[A-Z])(?:([A-Z\d])(?!.*\1)){5,20}$
You can try this,
^(?!^0+$)[a-zA-Z0-9]{3,20}$
Or
^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]$
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have two words within sentence.
EX: big BUTTON
here I need to check second word is in uppercase using regex expression.
Square brackets [] are your friend. They allow you to specify characters that will match. To match the first work, you need to check for any letter. This can be done with [a-zA-Z]. This will match any letter between a and z, as well as A and Z. for the second word, you only want to match uppercase, so only use [A-Z]. To get 1 or more matches, put a + after the closing bracket.
Putting this all together, with a space in between the words, you get [a-zA-Z]+ [A-Z]+.
The carat ^ is used to signify the start of the string, and the dollar sign $ is used to signify the end of the string. Your question somewhat vague, so here are a couple scenarios:
Each sentence is only two words with a space in between them: ^[a-zA-Z]+ [A-Z]+$
Each sentence has at least two words and may or may not end in a period: ^[a-zA-Z]+ [A-Z]+( |\.?$)
In the second example the parenthesis with a pipe (|) is used as an OR statement. The period is escaped since it is a special character (matches any single character). The question mark denotes 0 or 1 of the preceding character, which is a period. So ( |\.?$) will match a space or a sentence that ends with or without a period.
Here is a good site that has information on Regexes: http://www.regular-expressions.info/
This regexp looks for any sequence, starting at the beginning of the string (^), of alphanumeric characters (\w)--that's the first word--then a space, followed by a sequence of upper-case letters ([A-Z]+)--the second word--followed by either a space or the end of the string ($).
/^\w+ [A-Z]+( |$)/
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a basic regex that should return string after the last backslash \.
Regex :
/([^\\]*$)/
Works fine in Regex101.
Output :
random.html
But not in Javascript example bellow :
console.log("C:\fakepath\extra\random.html".match(/([^\\]*$)/));
Output :
["C:akepathextra
andom.html", "C:akepathextra
andom.html", index: 0, input: "C:akepathextra
andom.html"]
The problem is not with the RegEx, it's with the string itself. In JavaScript strings \ is used to escape the following character.
The string
"C:\fakepath\extra\random.html"
is after escaping
C:akepathextra
andom.html
To use backslash in the string, escape them by preceding backslash.
"C:\\fakepath\\extra\\random.html"
console.log("C:\\fakepath\\extra\\random.html".match(/[^\\]*$/));
To get the text after last \, use String#split and Array#pop
"C:\\fakepath\\extra\\random.html".split('\\').pop() // random.html
^^ Note: this backslash also need to be escaped.
[^\\]* match a single character not present in the list.
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed
The problem is not with the RegEx, it's with the string itself. In JavaScript strings \ is used to escape the following character.
To use backslash in the string then escape them by preceding backslash.
"C:\\fakepath\\extra\\random.html"
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I do not know if this is nonsense but I need a regular expression to validate the SKU of a product, taking into account that it must be 10 characters minimum and 20 maximum and allow [0-9] and [a-zA-Z] but I have not the slightest idea, the regular expression is to be used from Javascript with . test(), any help?
You can use character class and specify length range for this.
A character class is a set of characters (or character range) enclosed in a pair of square brackets like [abc] or [A-Z].
So for your case, it would be [A-Za-z\d] which denotes an upper case or lower case letter or a digit. \d stands for digit.
For allowing n occurrence of any pattern, you can use <your_pattern>{n}. If it is a range from m to n, use <your_pattern>{m,n}. For specifying only minimum, use <your_pattern>{n,} and for specifying only maximum, use <your_pattern>{,n}.
Using a service for regex is helpful.
This should be what you need
^[a-z0-9A-Z]{10,20}$
if it is just JavaScript you can set a flag for upper and lower case
/^[a-z0-9]{10,20}$/i
Example below.
http://regex101.com/r/uE8fH1