This question already has answers here:
Regular expression to match any character being repeated more than 10 times
(8 answers)
Closed 4 years ago.
I'm trying to find a regex which finds 3 repeating characters appearing contiguously in a string. The character set could be alphabet, digit or any special character.
I wanted the first try for alphabet and digits and then extend the expression to include special characters. The ones I tried are. Both of these fail for the string "c2sssFg1". What am I doing wrong here?
(\\w*)\\2{3,}(\\w*)
(\\w*?)(\\w)\\2{3,}(\\w*)
I looked at some of the examples on SO and on web but I didn't find the right solution that passes the random strings I test.
Can someone help me with this?
Thanks.
(.)\1{2}
(.) matches any char
\1 matches that exactly char
{2} is to grant its 2 more of that
Try (.)\1\1. It works for general case.
Related
This question already has answers here:
Regular expression for first and last name
(28 answers)
Closed 2 years ago.
I'm looking for a solution in regex. I have a full name for which the allowed characters are a-z, A-Z, space and /,-.#'
Also it should not start with a blank character/space.
So basically the following names are accepted.
Anjith Sasindran
Anjith# Sasindran'
Anjith
Anjith/,Sasi#n'ran
An-. Sasindr#
And the following are not
An%jith Sasindran
Anjit*) Sasindran
Basically anything other than the ones I listed above.
I'm not sure how to do the same. I've very little knowledge in regex b/w. So any help would be appreciated.
use:
^[^\s][ A-Za-z-'#.,/]*$
^[^\s] checks that there is no whitespace at the beginning
A-Za-z accepts all the alphabets in the given string
-'#.,/ accepts only these special characters
* matches 0 or more preceding token
With regular expressions, you can define classes of characters like the follwing:
[\sa-zA-Z#\.,\-'/].
With that, you define one "allowed" character. You can specify a sequence of it by adding plus operator : [\sa-zA-Z#\.,\-'/]+.
Now, to avoid texts starting with blank character, you can create a character class with all wanted characters except spaces [a-zA-Z#\.,\-'/].
So, now, the final regex is "any of the wanted characters except a blankspace, followed by any of wanted characters a certain number of times" : [a-zA-Z#\.,\-'/][\sa-zA-Z#\.,\-'/]*
With java, you must use Pattern class to apply regex checks on a string:
var nameMatcher = Pattern.compile("[a-zA-Z#\\.,\\-'/][\\sa-zA-Z#\\.,\\-'/]*").asPredicate();
if (nameMatcher.test("Anjhit#")) System.out.println("Name match !");
You can get a lot of information with Oracle documentation.
This question already has answers here:
How to match letters only using java regex, matches method?
(5 answers)
Closed 3 years ago.
I need a regular expression which should have at least 2 letters.
Total length should be between 6 & 50 characters and can as well include these special characters - _ . #
I came up with this line but it still accepts when I have in the input 1 letter and one special character with a minimum of 6 characters in total.
Where there should be 2 letters minimum for the input to be valid.
^(?=(?:\d*\D){2})[a-zA-Z0-9-_#\\.]{6,50}$
I expect it to be valid when I give for example : a12#45b
And expect to be invalid when I give for example : a1234# (this output gives me valid with expression I have now)
Matching not a digit \D does not guarantee a character a-z.
You could update the positive lookahead to assert 2 times not a-zA-Z and then a-zA-Z. Note that you don't have to escape the dot in the character class.
^(?=(?:[^a-zA-Z]*[A-Za-z]){2})[a-zA-Z0-9-_#.]{6,50}$
Or as suggested in the comment by #Thomas make the pattern case insensitive and use only a-z:
^(?i)(?=(?:[^a-z]*[a-z]){2})[a-z0-9-_#.]{6,50}$
This question already has answers here:
Using Regular Expressions to Extract a Value in Java
(13 answers)
Closed 3 years ago.
This is intended to be used in Java.
Imagine following sample input:
WRA1007
1085808
1092650S
3901823CV
I want to match all alphabetic characters after at least one digit.
Desired output:
S
CV
Actual output:
0S
3CV
My current approach looks like this:
\d[a-zA-Z]+
The problem with this pattern is that it includes the digit beforehand too. My current solution is to remove the first character of the resulting string afterwards. And this seems quite unsatisfactory to me.
You need a lookbehind:
(?<=\d)[a-zA-Z]+
(?<=\d) means "there must be a digit before this position, but don't match it".
Demo
Alternatively, you can use a pair of () to surround the part you want to get:
\d([a-zA-Z]+)
This is called a "group", and you can get its value by calling group(1) on your Matcher.
If you 'add' groups you can get group 1 that contain only letters
\d([a-zA-Z]+)
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 6 years ago.
I am trying to solve following task:
Match the pattern abc.def.ghi.jkl, where each variable a,b,c,d,e,f,g,h,i,j,k,l can be any single character except the newline.
For above question I am matching the input to regex :
"([^\\n]{3}(.)){3}([^\\n]{3})"
// this is the regex pattern I am using currently
What am I doing wrong? Please help me correct the above regex so that it does not match the incorrect input I have provided in the title. Currently it matches to it somehow. Although I have provided 3 it is apparently matching to more than 3 characters.
. has a special meaning in regular expression patterns.
If you want to get a "simple dot", you need to quote/escape it (as "\\.").
And that special meaning is (under normal configuration) "any character except line breaks", which exactly matches your other condition, so you can simplify this to
"(...)\\.(...)\\.(...)\\.(...)"
This question already has answers here:
Regex to accept alphanumeric and some special character in Javascript? [closed]
(2 answers)
Closed 7 years ago.
I would need a Regex which allows only uppercase letters, number and two special characters.
so far I have the Regex for the letters and numbers.
^[A-Z0-9]+$
I would also like to allow the usage of the "-" and "#" symbols
like
A453#
A-59#
for example
Any advices?
Cheers
^[A-Z0-9#-]+$
This should do it for you.
Often numbers must start form a letter (e.g. "A1234-5" is allowed when "###" is ruled out), if it's your case, the pattern is
^[A-Z][A-Z0-9#-]*$