Hi I am trying to find a regEx which only accepts lower case letters nothing else,
I tired few options nothing worked, Can some one please guide me :
here are the expressions I tried :
(?=.*[a-z])--> this is not working if we have "aaaAA"
[a-z]+^[A-Z]
^[A-Z]+[a-z]+^[A-Z]
Please help me.
Did you try just [a-z]+
See example here:
https://regex101.com/r/wCdBue/1
If you want to check if whole line / text contains only lowercase letter you should wrap your regex with ^$ like this: ^[a-z]+$
Example:
https://regex101.com/r/T0DHY4/1
Your solution is nearly correct. The regex must say "[a-z]+"—include a quantifier, which means that you are not matching a single character, but one or more lowercase characters.
String regex = "[a-z]+";
Here is a quantifier regex link.
http://docs.oracle.com/javase/tutorial/essential/regex/quant.html
It's working for me like this:
String line = "aaaA";
System.out.println(line.matches("^[a-z]+$")); //false
line = "aaaa";
System.out.println(line.matches("^[a-z]+$")); //true
Related
I am looking for a regular expression that can strip all 'a' characters from the beginning of an input word (comprising only of English alphabet).
How would I do this using an regular expression?
The following look behind based regex fails to do the job:
(?<=a*?)(\w)+
as for input abc the above regular expression would return abc.
Is there a clean way to do this using lookbehinds?
A (brute force-ish) regular expression that does work is using negation:
(?<=a*)([[^a]&&\w])*
which returns the correct answer of bc for an input word abc.
But I was wondering if there could be a more elegant regular expression, say, using the correct quantifier?
Pattern removeWords = Pattern.compile("\\b(?:a)\\b\\s*", Pattern.CASE_INSENSITIVE);
Matcher fix = removeWords.matcher(YourWord);
String fixedString = fix.replaceAll("");
this will remove a from the current string and if you want to remove some other letters
Pattern removeWords = Pattern.compile("\\b(?:a|b|c)\\b\\s*",Pattern.CASE_INSENSITIVE);
you ca do it this way
I think that a regex for this problem is overkill.
You could instead do:
str = str.startsWith("a") ? str.substring(1) : str;
Try with:
(?i)\\ba?(\\w+)\\b
and replace a word with captured group 1.
Code example:
String word = "aWord Another";
word = word.replaceAll("(?i)\\ba?(\\w+)\\b", "$1");
System.out.println(word);
with output:
Word nother
There are much more simpler way to do this, but as you insist on using using lookbehinds, I will give one. The regex will be
(?<=\b)a+(\w*)
Regex Breakdown
(?<=\b) #Find all word boundaries
a+ #Match the character a literally at least once. We have already ensured using word boundary to find those a's only which are starting of word
(\w*) #Find remaining characters
Regex Demo
Java Code
String str = "abc cdavbvhsza aaabcd";
System.out.println(str.replaceAll("(?<=\\b)a+(\\w*)", "$1"));
Ideone Demo
I'm trying to validate the given string. It should allow (a-z, white space between words,commas,dots only).
I tried this but it doesn't work:
final String name = "/^[a-z]+(\s+[a-z]+)*$/i";
Pattern pattern = Pattern.compile(name );
Matcher matcher = pattern.matcher("Ravi is a java developer");
System.out.println("***********"+matcher.matches());
please suggest to me.
Thanks in advance.
The regex and example you provided will not work, as you don't allow capital letters.
You can try this:
([a-zA-Z]*(\s)*[\.\,]*)*
You need to enable case insensitive modifier (?i)
String s = "Ravi is a java developer";
System.out.println(s.matches("(?i)[a-z]+(\\s+[a-z]+)*")); // true
OR
System.out.println(s.matches("(?i)[a-z]+([,.\\s]+[a-z]+)*")); // true
This Should work:
String name = "[a-zA-z]*([,.\\s]+[a-z]*)*"
And this should be helpful: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Why complicate?
"^A-Z[a-z \\.,]+$" <-- something that starts with a capital letter and has only letters, space, dots and commas after that.
I'm surprised how complicated regexes the average person creates.
I want to remove the sequesnce "-~-~-" if it repeats in a string, but only if they are together.
I have tried to create a regex based on the removing of multiple white spaces regex:
test.replaceAll("\\s+", " ");
Unfortunately I was unsuccessful. Can someone please help me write the correct regex? thanks.
Example:
string test = "hello-~-~--~-~--~-~-"
output:
hello-~-~-
Another example
string test = "-~-~--~-~--~-~-hello-~-~--~-~--~-~-"
output:
-~-~-hello-~-~-
The regex is:
test.replaceAll("(-~-~-){2,}", "-~-~-")
replaceAll replaces all occurrences matched by the regex (the first parameter) with the second parameter.
the () groups the expression -~-~- together, {2,} means two or more occurrences.
EDIT
Like #anubhava said, instead of using -~-~- for the replacement string, you could also use $1 which backreferences the first capturing group (i.e. the expression in the regex surrounded by ()).
test.replaceAll("(-~-~-)+", "-~-~-");
This is the regex you need:
(-~-~-){2}
I want to create a regex which matches a word in a String:
Miete 920
I want to match the word "Miete".
My regex:
price.matches("=[\bMiete\b]")
However, it doesn`t work? Pls give me a hint.
If you want to check if some string contains separate word Miete you can use
price.matches(".*\\bMiete\\b.*");
There is no need for = in your regex, also [...] is character class not string literal.
I think your regex is wrong. Try with
price.matches(".*\\bMiete\\b.*")
.* -> 0 or more charcters
\\b -> word boundary
So this will match any string that has Miete surrounded by word boundaries.
EDIT: sorry fixed, I forgot how matching works in Java, I'm more used to Perl :)
I am trying to write a regular expression to do a find and replace operation. Assume Java regex syntax. Below are examples of what I am trying to find:
12341+1
12241+1R1
100001+1R2
So, I am searching for a string beginning with one or more digits, followed by a "1+1" substring, followed by 0 or more characters. I have the following regex:
^(\d+)(1\\+1).*
This regex will successfully find the examples above, however, my goal is to replace the strings with everything before "1+1". So, 12341+1 would become 1234, and 12241+1R1 would become 1224. If I use the first grouped expression $1 to replace the pattern, I get the wrong result as follows:
12341+1 becomes 12341
12241+1R1 becomes 12241
100001+1R2 becomes 100001
Any ideas?
Your existing regex works fine, just that you are missing a \ before \d
String str = "100001+1R2";
str = str.replaceAll("^(\\d+)(1\\+1).*","$1");
Working link
IMHO, the regex is correct.
Perhaps you wrote it wrong in the code. If you want to code the regex ^(\d+)(1\+1).* in a string, you have to write something like String regex = "^(\\d+)(1\\+1).*".
Your output is the result of ^(\d+)(1+1).* replacement, as you miss some backslash in the string (e.g. "^(\\d+)(1\+1).*").
Your regex looks fine to me - I don't have access to java but in JavaScript the code..
"12341+1".replace(/(\d+)(1\+1)/g, "$1");
Returns 1234 as you'd expect. This works on a string with many 'codes' in too e.g.
"12341+1 54321+1".replace(/(\d+)(1\+1)/g, "$1");
gives 1234 5432.
Personally, I wouldn't use a Regex at all (it'd be like using a hammer on a thumbtack), I'd just create a substring from (Pseudocode)
stringName.substring(0, stringName.indexOf("1+1"))
But it looks like other posters have already mentioned the non-greedy operator.
In most Regex Syntaxes you can add a '?' after a '+' or '*' to indicate that you want it to match as little as possible before moving on in the pattern. (Thus: ^(\d+?)(1+1) matches any number of digits until it finds "1+1" and then, NOT INCLUDING the "1+1" it continues matching, whereas your original would see the 1 and match it as well).