Conditions :
Contains only alphabet, hyphens and apostrophes.
Contains at least 2 consecutive letters
Does not start or end with space or hyphen
Contains minimum length of 2.
Valid: a-gm-k,ak,h-'kj,um'h-k
Invalid: a-h-j,
Tried ^([a-zA-Z'][a-zA-Z'-]*[a-zA-Z'])$
This is filing for the consecutive condition
Wrote \\b([a-zA-Z])\\1+\\b but this is failing if I give aa
Can somebody please advise how to write a single regex expression for above conditions?
A sequence of positive lookaheads (?=) needs to be applied:
(?=([-'A-Za-z]{2,}))(?=^[^- ].*[^- ]$)(?=.*[A-Za-z]{2,}.*)
where:
(?=([-'A-Za-z]{2,})) - contains only hyphen, apostrophe, or letters, minimum length of 2
(?=^[^- ].*[^- ]$) - does not start or end with - or space
(?=.*[A-Za-z]{2,}.*) - contains at least two consecutive letters
Online demo: Regexplanet
Online demo: regex101.com
I am writing a regex to match the string containing more than 2 words and should have at least 1 digit available or 1 word with no digits.
i.e If I have following strings:
1. "Sample data with no digit" (no digit)
2. "1004" (less than 2 words)
3. "1004 1008" (no alphabets)
4. "1004 data" (exactly 2 words)
5. "5ample Data with digits" (note that S-> 5)
6. "Sample Data with 1004"
The regex should match the 5th,6th strings (reason for not fetching others is mentioned along with the data)
I tried following but the following always returns all the strings:
[\d[0-9]|[ABEGFIHKJLOQPSRUTXZbgfihkjloqpsuz!]]+[\w\s]* (returns all strings)
Please note that I am using JAVA.
Please help and thanks in advance.
You can use this regex with 2 lookahead assertions:
^(?=.*\b[a-zA-Z]*\d+[a-zA-Z]*)(?=.*\b[a-zA-Z]+\b)(?:\w+\h+){2,}\w+
RegEx Demo
RegEx Breakup:
(?=.*\b[a-zA-Z]*\d+[a-zA-Z]*): Lookahead to ensure we have a word with a digit
(?=.*\b[a-zA-Z]+\b): Lookahead to assert we have a word with no digit
(?:\w+\h+){2,}\w+: Make sure we have at least 3 words in input
I have to make a regex which selects the first number only if there are 3 numbers in a row in the beginning of the string.
Example:
012 test
Here I'd like to match the 0 at the beginning of the string.
02 test
Here I want nothing to be matched by the regex.
I have no idea how to check the string without matching it using regex. Is this possible?
Use positive lookahead assertion to check which follows two digits and start anchor(^) to start at the beginning of the string.
/^\d(?=\d{2})/
UPDATE : You can use word boundary(\b) to avoid matching when it follows any word character after the two digits.
/^\d(?=\d{2}\b)/
Maybe this is asked somewhere but certainly I couldn't find the answer I want so:
I'm having difficulties to match specific characters in a string:
"88551554,86546546,51516565"
The digits I want to match are the X's in the following :
"XXXXX554,XXXXX546,XXXXX565"
Right now I'm only able to find out the last 3 digits before each comma :
\d{3}(?=,)
And since the length of the numbers are dynamic, it seems not possible to specify the number of digits before the 3 digits.
Anyone can help?
Thanks in advance!
You can use this lookahead regex:
(\d+)(?=\d{3}(?:,|$))
RegEx Demo
This will match and group 1 or more digits that must be followed by 3 digits and a comma or end of input. Check MATCH INFORMATION in the demo link for captured groups.
Update: To replace all those matched digits by X use:
str = str.replaceAll("\\d(?=\\d*\\d{3}(?:,|$))", "X");
RegEx Demo2
To match it use:
\d+(?=\d{3})
This regex does:
\d+... Match a digit (0-9) between one and unlimmited times.
(?=\d{3}) ... Match a digit (0-9) exactly three times inside an positive lookahead.
I am trying to write a regex for java that will match the following string:
number,number,number (it could be this simple or it could have a variable number of numbers, but each number has to have a comma after it there will not be any white space though)
here was my attempt:
[[0-9],[0-9]]+
but it seems to match anything with a number in it
You could try something along the lines of ([0-9]+,)*[0-9]+
This will match:
Only one number, e.g.: 7
Two numbers, e.g.: 7,52
Three numbers, e.g.: 7,52,999
etc.
This will not match:
Things with spaces, e.g.: 7, 52
A list ending with a comma, e.g.: 7, 52,
Many other things out of the scope of this problem.
I think this would work
\d+,(\d+,)+
Note that as you want, that will only capture number followed by a comma
I guess you are starting with a String. Why don't you just use String.split(",") ?
^ means the start of a string and $ means the end. If you don't use those, you could match something in the middle (b matched "abc").
The + works on the element before it. b is an element, [0-9] is an element, and so are groups (things wrapped in parenthesis).
So, the regex you want matches:
The start of the string ^
a number [0-9]
any amount of comas flowed by numbers (,[0-9])+
the end of the string $
or, ^[0-9](,[0-9])+$
Try regex as [\d,]* string representation as [\\d,]* e.g. below:
Pattern p4 = Pattern.compile("[\\d,]*");
Matcher m4 = p4.matcher("12,1212,1212ad,v");
System.out.println(m4.find()); //prints true
System.out.println(m4.group());//prints 12,1212,1212
If you want to match minimum one comma (,) and two numbers e.g. 12,1212 then you may want to use regex as (\d+,)+\d+ with string representation as \\d+,)+\\d+. This regex matches a a region with a number minimum one digit followed by one comma(,) followed by minimum one digit number.