Regex to match only the following strings - regex

I have a few strings and I need some help with constructing Regex to match them.
The example strings are:
AAPL10.XX1.XX2
AAA34CL
AAXL23.XLF2
AAPL
I have tried few expressions but couldn't achieve exact results. They are of the following:
[0-9A-Z]+\.?[0-9A-Z]$
[A-Z0-9]*\.?[^.]$
Following are some of the points which should be maintained:
The pattern should only contain capital letters and digits and no small letters are allowed.
The '.' in the middle of the text is optional. And the maximum number of times it can appear is only 2.
It should not have any special characters at the end.
Please ask me for any clarification.

You can write the pattern as:
^[A-Z\d]+(?:\.[A-Z\d]+){0,2}$
The pattern matches:
^ Start of string
[A-Z\d]+ Match 1+ chars A-Z or a digit
(?:\.[A-Z\d]+){0,2} Repeat 0 - 2 times a . and 1+ chars A-Z or a digit
$ End of string
Regex demo

Related

Regex matching digit and letters or only digits

I'm stuck trying to code a regex which match with those conditions:
string with more than 9 digits
string with more than 9 digits and letters
I can't figure out how to write my regex saying to it: digit or digit and letters can match but not letters.
Those string should match:
12345678987654567
jhsjd4567hsqdgqsgh456786576567kj
9l8j9n9k0n9n8n
Those string should not match:
loremipsum
a1
12567
My regex so far: /(?:\w){9,}/
Thanks a lot :)
I am interpreting your requirements as: match a string of more than nine characters which contains either digits only or digits and letters only.
const tests = [
'12345678987654567',
'jhsjd4567hsqdgqsgh456786576567kj',
'9l8j9n9k0n9n8n',
'loremipsum',
'a1',
'12567',
];
for (const t of tests) {
console.log(t.padEnd(35) +
/^(?=.*\d)[a-z\d]{10,}$/i.test(t)
)
}
The positive lookahead (?=.*\d) ensures that there is at least one digit in the string.
Remove the i flag if you want to match only lower-case letters.
Using \w can match both letters and digits, so using (?:\w){9,}, which can be written as \w{9,} can also match only letters or only underscores.
Reading the requirements, you can match 9 or more times a letter or a digit and make sure that the string does not contain only letters using a negative lookahead if that is supported.
If you want to match more than 9, you can use {10,} as the quantifier.
^(?![a-zA-Z]+$)[a-zA-Z0-9]{9,}$
The pattern matches:
^ Start of string
(?![a-zA-Z]+$) Negative lookahead, assert not only characters a-z A-Z in the string
[a-zA-Z0-9]{9,} Match 9 or more times chars a-z A-Z or a digit
$ End of string
Regex demo
Or using word boundaries:
\b(?![a-zA-Z]+\b)[a-zA-Z0-9]{9,}\b
Regex demo
I believe it should work
/([a-z]*[0-9][a-z]*){9,}/

RegEx: How to match a whole string with fixed-length region with negative look ahead conditions that are overriden afterwards?

The strings I parse with a regular expression contain a region of fixed length N where there can either be numbers or dashes. However, if a dash occurs, only dashes are allowed to follow for the rest of the region. After this region, numbers, dashes, and letters are allowed to occur.
Examples (N=5, starting at the beginning):
12345ABC
12345123
1234-1
1234--1
1----1AB
How can I correctly match this? I currently am stuck at something like (?:\d|-(?!\d)){5}[A-Z0-9\-]+ (for N=5), but I cannot make numbers work directly following my region if a dash is present, as the negative look ahead blocks the match.
Update
Strings that should not be matched (N=5)
1-2-3-A
----1AB
--1--1A
You could assert that the first 5 characters are either digits or - and make sure that there is no - before a digit in the first 5 chars.
^(?![\d-]{0,3}-\d)(?=[\d-]{5})[A-Z\d-]+$
^ Start of string
(?![\d-]{0,3}-\d) Make sure that in the first 5 chars there is no - before a digit
(?=[\d-]{5}) Assert at least 5 digits or -
[A-Z\d-]+ Match 1+ times any of the listed characters
$ End of string
Regex demo
If atomic groups are available:
^(?=[\d-]{5})(?>\d+-*|-{5})[A-Z\d_]*$
^ Start of string
(?=[\d-]{5}) Assert at least 5 chars - or digit
(?> Atomic group
\d+-* Match 1+ digits and optional -
| or
-{5} match 5 times -
) Close atomic group
[A-Z\d_]* Match optional chars A-Z digit or _
$ End of string
Regex demo
Use a non-word-boundary assertion \B:
^[-\d](?:-|\B\d){4}[A-Z\d-]*$
A non word-boundary succeeds at a position between two word characters (from \w ie [A-Za-z0-9_]) or two non-word characters (from \W ie [^A-Za-z0-9_]). (and also between a non-word character and the limit of the string)
With it, each \B\d always follows a digit. (and can't follow a dash)
demo
Other way (if lookbehinds are allowed):
^\d*-*(?<=^.{5})[A-Z\d-]*$
demo

Regex 2 Numbers OR Special Characters

So i'm trying to create a Regex which does the following:
Min 12 Characters, Requires Uppercase, Requires Lowercase, Requires 2 Numeric values OR 2 Special Characters.
At the moment i have the following:
~^(?=\P{Ll}*\p{Ll})(?=\P{Lu}*\p{Lu})(?=.*[!##$%^&*()]|\D*\d).{12,}~u
Which does 1 numeric OR 1 special character, not 2. I've tried adding {2} to the OR condition, however, this requires a combination of two which is incorrect.
Any help would be appreciated.
You should replace (?=.*[!##$%^&*()]|\D*\d) lookahead with (?:(?=(?:[^!##$%^&*()]*[!##$%^&*()]){2})|(?=(?:\D*\d){2})). The regex will look like
'~^(?=\P{Ll}*\p{Ll})(?=\P{Lu}*\p{Lu})(?:(?=(?:[^!##$%^&*()]*[!##$%^&*()]){2})|(?=(?:\D*\d){2})).{12,}$~u'
See the regex demo.
The lookahead matches a location that is immediately followed with
(?:[^!##$%^&*()]*[!##$%^&*()]){2} - two repetitions of any 0+ chars other than !##$%^&*() chars followed with a char from the !##$%^&*() list
| - or
(?=(?:\D*\d){2} - two repetitions of any 0+ non-digit chars followed with a digit

regular expression matches wrong String

I would like to match String that contains letters and numbers WITHOUT space
I tried ^[a-zA-Z+d*]*$ but it matches String that have only letters
This is what it should do :
Nope
Nope 2
MatchPlease123
If you want to try in live:
http://rubular.com/r/pFMkk9ATc0
Thank you
You may use
/^(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[a-zA-Z0-9]*$/
See the regex demo (a bit modified since the input is a multiline string).
Details:
^ - start of a string
(?=[^a-zA-Z]*[a-zA-Z]) - a positive lookahead requiring that there must be at least one ASCII letter after any 0+ chars other than ASCII letters
(?=[^0-9]*[0-9]) - a positive lookahead requiring that there must be at least one ASCII digit after any 0+ chars other than ASCII digits
[a-zA-Z0-9]* - 0+ ASCII letters or digits
$ - end of string.
I think this simplest one will also be helpful
Regex demo
Regex: ^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]+$
1. ^ start of string.
2. (?=.*\d) positive look ahead for digit.
3. (?=.*[a-zA-Z]) positive look alphabets.
4. [a-zA-Z\d]+ match all digits A-Z and a-z
5. $ end of string.

match only letters after comma without numbers

im using regex to match certain text after selecting with xpath
for example Huntsville, Alabama 11111
i want only Alabama which always come after comma
and i use [^,]*$ to get text after comma
but i can't seem to find a way to exclude numbers or returns only the letters
another exmaple when i want to get the numbers after the comma i use [^[0-9],]*$
but when i tried to tweak it with anything else it only return numbers or nothing.
[?<=,\s*][a-zA-Z]+ You can try this.
Explanation:
?<= => lookbehind to match a string but not include in capture group
,\s* => match comma followed by 0 or more spaces
[a-zA-Z]+ => match letters only (one or more)
HTH
To match a letter word after the last comma, you may use
[a-zA-Z]+(?=[^,]*$)
See the regex demo.
Details
[a-zA-Z]+ - 1 or more ASCII letters
(?=[^,]*$) - followed with 0+ chars other than , up to the end of the string.
To match 1 or more words in the same context, use
[a-zA-Z]+(?:\s+[a-zA-Z]+)*(?=[^,]*$)
^^^^^^^^^^^^^^^^^
See this regex demo.
The (?:\s+[a-zA-Z]+)* part matches zero or more consequent occurrences of 1+ whitespaces and 1+ ASCII letters.