This question already has answers here:
Difference between regex [A-z] and [a-zA-Z]
(6 answers)
Closed 8 years ago.
I'm struggling with the following regexp
[A-z0-9]+
If tested against this string:
||a919238[.--a]asd|
it returns a919238[, including the square bracket.. I tried to input my test case on regex101 to understand what's wrong, but the site regex explanation is not helping, probably I'm not able to see my mistake.
Why is the square bracket included in the result?
Because
[A-z0-9]+
↑ ↑
is from A to z, see the ASCII table, ] appears between the two characters:
A===>64
z===>122
[===>91
So it is in between the range you have defined.Use [A-Za-z0-9]+
You can use /[a-z0-9]+/i (the i makes it case-insensitive), or /[A-Za-z0-9]+/.
Related
This question already has answers here:
How can I "inverse match" with regex?
(10 answers)
Closed 6 months ago.
Regex: /^[0-9\p{L}.,\s]+$/u
I would like to replace the characters not matching with the regex with "".
As I understand, you simply want to drop all chars not matching your regex. So the idea is to invert the class of chars:
/^[0-9\p{L}.,\s]+$/u should become /[^\d\p{L}.,\s]+/gu (I added the ^ after the [ to say "not in this list of chars" and replaced 0-9 by \d for digits. Use the g modifier (=global ) to match multiple times.
Running it: https://regex101.com/r/IQz6K5/1
I'm not sure that ,, . and the space will be enough ponctuation. It would be interesting to have a complete example of what you are trying to achieve. You could use another unicode character class for ponctuation if needed, typically with \p{P}. See more info about unicode classes here: https://www.regular-expressions.info/unicode.html#category
This question already has an answer here:
Regex match strings between asterisks
(1 answer)
Closed 2 years ago.
Is it possible to do a RegEx to only match the last asterisk right before and first asterisk right after an value?
Value: **example**
My actual regex: /\*(.*?)\*/g
My actual regex matches the two asterisks before and after word example, but i want to do a regex that only matches the last asterisk right before and the first asterisk right after the word.
Thank you!
try /[*][^*]+[*]/gm
Works as tested on regex101.com
This question already has answers here:
Regex plus vs star difference? [duplicate]
(9 answers)
Closed 2 years ago.
I am expecting the following regex to allow 0-n spaces between operators, but it is forcing at least one. Can someone please correct the error of my ways?
((\d+\.?\d*|\d*\.?\d+\s+?[\+\-\/\*]\s+?)+)(\d+\.?\d*|\d*\.?\d+)
Examples
24*3.2
24 * 3.2
Only the 2nd example is allowed through.
I understood \s+? should be an optional number of spaces?
Playpen
+? (or more generally ? following any other quantifier) is a non-greedy quantifier. It does not mean “make the preceding match optional”.
Use * instead of +?.
What you need is \s*, i.e., 0 or more white spaces.
Your \s+? says 1 or more white spaces and non greedy match for spaces.
The ? modifier after * or + means non-greedy match.
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
How to negate specific word in regex? [duplicate]
(12 answers)
Closed 3 years ago.
I want to validate a string with alpha numeric values, but if the string contains -- (double dash) anywhere in the string, it should be invalid.
valid:
apple123
-apple-123
app-le123
a-p-p-l-e-1-2-3
invalid:
--apple123
app--le123
https://stackoverflow.com/a/1240365/1920590
The above old post have the answer ^(?!.*bar).*$ which does the negation, but it does not work for same character repetition like --.
Can anyone help me to figure out to modify the ^(?!.*bar).*$ to identify -- as a string.
You may use a negative lookahead:
^(?!.*--)[\w-]+$
(?!.*--) is a negative lookahead assertion that will fail the match if -- appears anywhere in input.
[\w-] matches a word character [a-zA-Z0-9_] or a hyphen
RegEx Demo
This question already has answers here:
regex: required character in brackets
(3 answers)
Closed 4 years ago.
I am working for something and writing a regular expression to capture a string which is either (numbers and letters) or only numbers.
I know a regex for only number is [0-9] and alphanumeric is [A-Za-z0-9] . But this would capture even the strings which are only letters. How do i force it to not have only letters? Is there a way to do it in a single regex?
([0-9]*[a-zA-Z]*[0-9])+([a-zA-Z]*)
This should solve your problem.
You can test it here