My regex code [A-Z]{1,}\d{3,}\w? works fine returning strings like CX3623, M3326, Y2362 but I also want to be able to return strings which are in the following format:
YH321-2
V2021/V2022
1.2A-2351
YGH256-4268
What should I add to the regex?
Demo: https://regex101.com/r/MjPkFh/2
For the first part, you could match the different formats using an alternation.
You could make the second part optional using an optional non capturing group (?:...)? and match either / or - optionally followed by chars A-Z and 1+ digits.
\b(?:[A-Z]+ )?(?:[A-Z]*\d{3,}|\d+(?:\.\d+)?[A-Z]+)(?:[\/-][A-Z]*\d+)?\b
Explanation
\b Word boundary
(?:[A-Z]+ )? Optionally match 1+ chars A-Z followed by a space
(?: Non capture group
[A-Z]*\d{3,} Match 0+ times A-Z and 3 or more digits
| Or
\d+(?:\.\d+)?[A-Z]+ Match 1+ digits with an optional decimal part and 1+ times A-Z
) Close group
(?: Non capture group
[\/-][A-Z]*\d+ Match either / or -, 0+ times A-Z and 1+ digits
)? Close group and make optional
\b Word boundary
Regex demo
Related
^AB+[0-9A-Z?]+$
Javascript Regex Starts with AB 0-9 and then ends with A-Z as optional it could be AB00001 or AB00001A (as optional)
Strings which should work AB00001 , AB000001B , AB12122 , AB00001C with any capital letter as a n optional
The pattern that you tried ^AB+[0-9A-Z?]+$ matches:
Single A char
1+ times B char
1+ times any of the specified ranges 0-9 A-Z or a ? char.
It can match for example ABB???
You could write the pattern matching a single A and single B char, then 1+ digits and optionally match a char A-Z according to the example data.
^AB\d+[A-Z]?$
See a regex demo
I have a long text in form of a string.
This text includes a lot of questions that are at the same time the headers of sections.
These headers always start with a number+dot+whitespace character combination and end with a question mark, I am trying to extract these strings.
This is what I've got so far: longString.match(/\d\.\s+[a-zA-Z]+\s\\?/g).
Sure enough this doesn't work.
In your example you use [a-zA-Z]+, but you might extend that to matching 1 or more word characters using \w+
This part at the end of the pattern \s\\? matches an expected whitespace char followed by an optional backslash.
To match multiple words, you can optionally repeat the pattern to match a word preceded by 1 or more whitespace characters.
You one option is to use
\d\.\s+\w+(?:\s+\w+)*\s*\?
Explanation
\d\. Match a single digit (for 1 or digits use \d+)
\s+\w+ Match a . and 1+ whitspace chars and 1+ word chars
(?:\s+\w+)* Optionally repeat 1+ whitspace chars and 1+ word chars
\s*\? Match 0+ whitespace chars and a question mark.
Regex demo
A broader match might be matching at least a single time any char except a question mark or whitespace char after the digit, dot and whitespace:
\d\.\s+[^\s?]+(?:\s+[^\s?]+)*\?
Regex demo
I have a regular expression that allows only letters, numbers, spaces or hyphens. However, I'd like to disallow the user to do the following:
hello--world Have more than one hyphen sitting next to each other
--hello Have a hyphen in the beginning. It must have a number or letter first
How do I accomplish this? My current regex looks like this:
let alphanumericTest = new RegExp("^\s*([0-9a-zA-Z- ]*)\s*$");
You can try this regex expression. ^\s*[0-9a-zA-Z](?:(?!--)[0-9a-zA-Z- ])*$
This is a demo.
You could make you match a bit more efficient without using a negative lookahead for matching non consecutive hyphens using repeating groups which can optionally start with an hyphen after the first word.
^[ ]*[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)*-?(?:[ ]+-?(?:[0-9a-zA-Z]+-?)*)*$
(Used [ ] to match a space for clarity)
Explanation
^ Start of string
[ ]* Match 0+ spaces
[0-9a-zA-Z]+ Match 1+ times any of the listed
(?:-[0-9a-zA-Z]+)* Repeat 0+ times matching a hyphen and 1+ what is listed
-? Match optional hyphen
(?: Non capturing group
[ ]+-?(?:[0-9a-zA-Z]+-?)* Match 1+ spaces, optional hyphen, repeat 0+ times what is listed and optional hyphen
)* Close outer non capturing group and repeat 0+ times
$ End of string
Regex demo
Try:
let alphanumericTest = new RegExp("^(?!-)(?!.*--)[0-9a-zA-Z- ]+(?<!-)$");
This checks that the first character is not a - and that there are no consecutive --s anywhere in the string
I need a RegEx that allow the strings that start with numbers separated by comma, finishes with a number (or withspaces after the number) and allow also whitespaces between the number and the comma.
E.g. var str= '1 , 8,9, 88' has to be accetpted while var str2="1 2, 5" has not to be accetped. I tried with var regEx= "^[0-9\,\s]+$"but doing like this it accepts the strings that end with a comma and the strings that have two numbers not separated by comma. Any ideas?
EDIT:
Example of string accepted:
str1= "1,2,3,4"
str2= "1 , 2,3,9"
str3= " 8 , 44, 3 , 11"
Example of string to be discarded:
str4="1, 2,"
str5=", 1,2,"
str6="1,2 3,4"
You could account for the spaces before and after the comma using \s (or just match a space only because \s also matches a newline) to match a whitespace character and use a repeating pattern to match a comma and 1+ digits:
^\s*\d+(?:\s*,\s*\d+)*\s*$
^ Start of string
\s*\d+ Match 0+ whitespace chars and 1+ digits
(?: Non capturing group
\s*,\s*\d+ Match 0+ whitespace chars, and comma, 0+ whitespace chars and 1+ digits
)* Close non capturing group and repeat 0+ times
\s*$ Match 1+ whitespace chars and assert end of string.
Regex demo
Need some help in designing regular expression to validate hyphen separated floating point numbers in Javascript. So far I have managed to achieve this RegEx:
(^((\\d)+(\.[0-9]+)?)(\-)?((\\d)+(\.[0-9]+)?)$)|^(\\d+)$
It matches the following:
1) 2
2) 2.10
3) 3.10-3.14
The problem with this one is that its also matching "3.103.310" which is wrong number. Much appreciate any help in fixing this issue.
The problem comes from the first alternative that matches 1 or more digits with an optional fractional part ((\d)+(\.[0-9]+)?) and then matches a hyphen and again 1+ digits and again an optional fractional part. Thus, 2 dots are allowed.
You may fix the pattern like this:
^\d+(?:\.\d+)?(?:-\d+(?:\.\d+)?)*$
See the regex demo
Details
^ - start of string
\d+ - 1+ digits
(?:\.\d+)? - an optional non-capturing group:
\. - a dot
\d+ - 1+ digits
(?:-\d+(?:\.\d+)?)* - an non-capturing group matching 0+ occurrences of
- - a hyphen
\d+(?:\.\d+)? - 1+ digits and 1 or 0 occurrences of . and 1+ digits
$ - end of string