Regex to not have a sequence of repeated characters - regex

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}$"

Related

Regex expression for [number2] in [number],[number2][word]

I'm trying to find a regular expression to find [number2] in [number],[number2][word].
So far I've tried with [,](\d*), but it also gets me the comma.
Demo: https://regexr.com/59eqa
You may use:
(?<=,)(\d*)
Regex Demo
Detail:
(?<=,): positive look behind that doesn't consume character but indicate that the number must have , before it
The previous answers do not handle the case that the second (or two numbers) is matched.
If the second number must be captured, this can be done with
\b\d+,(\d+)[A-Za-z]
where the "number2" is contained in captured group 1.
If you want to get the match only, you could use 2 lookarounds, asserting a comma to the left and a char a-zA-Z to the right.
Use \d+ to match 1 or more digits.
(?<=,)\d+(?=[a-zA-Z])
Regex demo
If there should be a digit before the comma as well:
(?<=\d,)\d+(?=[a-zA-Z])
Regex demo

How to match digits and dots. It has to start with digits first

For this example hello.1.2.3.4.world I want to match a result which gives me 1.2.3.4. Number of digits between dots doesn't matter. As long as it follow digit.digit pattern
My part solution was following regular-expression [\d.]+.[^.a-z], which gives me .1.2.3.4 as result. And I strip the first dot by using trim or similar method.
Any regexp master who can tell me how to rid the first dot with one regular expression only?
How about this: \.(\d(?:\.\d)*)\.\D
EDIT:
(\d+(?:\.\d+)*)
Demo
If you want to use your current regex you can put a lookahead at the start, and escape the literal dot when not inside a character group (?=\d)[\d.]+\.[^.a-z]
The lookahead (?=\d) will make sure the first character matched is a digit.
Demo here

Regular expression spaces in words

Given:
<currency: word with spaces, 56>
and the regular expression:
<(?:CURRENCY):[ ]*(\w+(\s*,\s*)\d+(\s*\d+)*)>
What must I change to accept spaces in the "words with spaces"
You're currently searching for \w which is a word character, the opposite of whitespace. Also, not sure if you're intending to capture a whitespace and commas, instead of the number values. This captures only the word and the numbers.
<CURRENCY:\s+?(.+)\s*,\s*(\d+)(?:\s*(\d+))?>
I find regex101.com to be helpful when debugging these things.
does this help?
<(?:currency)\:\s*\w(\w|\s)+,\s*\d+(\s*|\d+)*>
This should do:
<currency: ((\w+\s*)*), (\d+)>
another one:
<(?:currency:)\s([\w\s]*),\s\d+>
if don't want to capture empty string, change * to +
Also you don't need the non capturing group ?:
<currency:\s([\w\s]*),\s\d+>
would do the same.
This worked for me:
<(currency):[ ]*(\w+(\s+\w+)*\s*,\s*\d+(\s*\d+)*)>
Not sure what "?:" does, so you may want:
<(?:CURRENCY):[ ]*(\w+(\s+\w+)*\s*,\s*\d+(\s*\d+)*)>

Regex matching groups by prefix

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 allow only one hyphen (-) in regex

I have a text box where i get the last name of user. How do I allow only one hyphen (-) in a regular expression?
^([a-z A-Z]*-){1}[a-z A-Z]*$
you can use negative lookahead to reject strings having more than one hyphen:
^(?![^-]+-[^-]+-)[a-zA-Z- ]+$
Matched demo on debuggex.
Another Matched demo on debuggex.
Not Matched Demo demo on debuggex.
Your regular expression allow exactly one -. but I assume that you want to mach "Smith", "Smith-Kennedy", but not "Smith-", to do this you just must move the hyphen to the second group:
^[a-z A-Z]+(-[a-z A-Z]+)?$
BTW, in almost all cases when * is used + is the better solution.
I am assuming you want up to 1 hyphen. If so, the regex you want is
^[a-z A-Z]*-?[a-z A-Z]*$
You can visualize it on www.debuggex.com.
If it matches .*-.*-, then you have more than one hyphen and such string should not be accepted
A problem with your regex is that it forces the user to put a -. You can use ? to make it optional :
^[a-z A-Z]*\-?[a-zA-Z]*$