I m looking for regex where it accepts alphanumeric characters and alphabets
Example
RAM123 -positive
ram123 - positive
rAm123-positive
Ram -positive
ram - positive
123-negative
I have tried using [0-9a-zA-Z].* but it also accepts numeric characters..which should not be..thr regex should accept any alphanumeric/ alphabets other than nums and special charcters
You need to use a negative lookahead assertion at the start.
^(?!\d+$)[\da-zA-Z]+$
DEMO
^[\da-zA-Z]*[a-zA-Z][\da-zA-Z]*$
You can simply use this.See demo.
https://regex101.com/r/nS2lT4/17
Related
I am trying to create a regex which matches the digits in the following Japanese strings,
4日
12日
while ignoring the following strings completely.
3月01日
3月1日
3月31日
So far, the closest I have been able to get is by using:
(?<!月)([0-9]{1,2})(?=日)
but this ends up matching the "1" contained in 3月01日 and 3月31日.
Any suggestions?
Add a digit pattern to the lookbehind:
(?<![0-9月])([0-9]{1,2})(?=日)
^^^
See the regex demo
The (?<![0-9月]) lookbehind will fail all the matches when the current position is preceded with a digit or 月 and backtracking won't return the partial numbers in the unwanted context.
I need regex to return invalid on a match. Specifically, the match is a string that starts with an A or an M and is followed by four numbers ie, A1223. The four numbers could be any random sequence.
I'm sure lookarounds are the way to handle this but I haven't grasped regex as a concept just yet. Thus far I've discovered how to capture the matched strings separate from other strings with the following.
([\s\S]*?)(A[\d][\d][\d][\d]|M[\d][\d][\d][\d])
Appreciate the help.
Regex doesn't really have match negation, but you can (ab)use a negative lookahead assertion to do inverted matching:
^((?!\s[AM]\d{4}).){6}
to match all strings not starting with A or M followed by 4 digits:
with negative lookahead:
^(?![AM]\d{4}).*
with consuming pattern using () capture groups:
[AM]\d{4}.*|(.+)
I current have this regex:
/^\+?\d+(\d|\-)+\d+$/
this accepts
12345
123-456
+12345
+12345-12345
my problem that this also accepts
123--123
123-------3242-324324
How can I fix the regex to not accept consecutive dash in between numbers?
This will be correct one
^\+?\d+(-\d+)*$
Regex Demo
or modifying a bit of your regex with negative lookahead will also work
^(?!.*--)\+?\d+(\d|\-)+\d+$
Regex Demo
I'm having the following string: CL_6x CL_5c CL_234 CL_ERB14 1D CL_6y
I need to find a regex to extract groups like this
CL_6x
CL_5c
CL_234
CL_ERB14 1D
CL_6y
As you can see they're all prefixed with CL_
Any ideas how to achieve this?
You need to use a positive lookahead based regex.
\bCL_.*?(?=\s*CL_|$)
This should match until the next CL_ or end of the line.
DEMO
CL_.+?\b
Try this.See demo.\b is word boundary
https://regex101.com/r/uF4oY4/86
EDIT:
for test cases like CL_ERB14 1D.
use
CL_\S+(?:\s*(?!CL_)\S+)
See demo.
https://regex101.com/r/uF4oY4/87
You can use following regex.
^CL_.+\b
Explanation
^: Starts with
CL_: Matches literal CL_
.+: Matches any characters any number of times
\b: Word boundary
I have a regex /(.)\1{1,2}/ that matches the text that has sequence of repeated characters.
But I want a regex that does the opposite. I don't want to negate it. How do can I do that?
(?!(.)\1{1,2}).
or you can try
(.)(?!\1)
You can try this.This uses a negative lookahead.See demo.
http://regex101.com/r/hQ1rP0/8
It couldn't be possible without negation,
(.)(?:(?!\1).){1,2}
DEMO
The below regex would capture the first character and checks for more than two repeated characters. If there are more than two repeated characters at the start, it won't match that string.
^(.)\1(?:(?!\1).)+$
DEMO
You can try this
"^(?!.*(.)\1).{0,11}$"