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 :)
Related
Input string
hello sworked? worked hello
output string
I need only work in worked.
I tried with this regex
(?!s)work
But this returns all work in both of sworked? and worked.
To match work not preceded with s use
(?<!s)work
See proof & explanation. (?<!s) is a negative lookbehind disallowing s right before work.
Or, match a word startign with work:
\bwork
See demo. \b is a word boundary.
Add \w* to match the rest of the word if necessary.
In Java, double backslashes (e.g. String regex = "\\bwork";).
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
I am trying to get following strings:
8374651-37374-19283-284745800
928S-29ED374-872B34-932837
26598TA-297374-CND283-16373
82911-LD391DB-632D-4927831
Looks like this by java regex:
XXXXXXX-XXXXX-XXXXX-284745800
XXXX-XXXXXXX-XXXXXX-932837
XXXXXXX-XXXXXX-XXXXXX-16373
XXXXX-XXXXXXX-XXXX-4927831
and there is no pattern about the length of string between each hyphen.
It could be easy to replace everything except hyphen with X but really difficult to me to exclude last part.
Use string.replaceAll function.
string.replaceAll("[^-\\n](?=.*?-)", "X");
This matches all the characters (but not of a hyphen) which was followed by a hyphen. It won't match the last part since it isn't followed by a hyphen.
DEMO
[^-\n](?=.*-[^-]*$)
Try this.Replace by X.See demo.
https://regex101.com/r/bW3aR1/18
For java it will be
[^-\\n](?=.*-[^-]*$)
The lookahead condition will select only those which are to be replaced by X.
or this pattern, replace w/ x
(?!-).(?=.*-)
Demo
I'm trying to match sentences without capital letters with regex in Java:
"Hi this is a test" -> Shouldn't match
"hi thiS is a test" -> Shouldn't match
"hi this is a test" -> Should match
I've tried the following regex, but it also matches my second example ("hi, thiS is a test").
[a-z]+
It seems like it's only looking at the first word of the sentence.
Any help?
[a-z]+ will match if your string contains any lowercase letter.
If you want to make sure your string doesn't contain uppercase letters, you could use a negative character class: ^[^A-Z]+$
Be aware that this won't handle accentuated characters (like É) though.
To make this work, you can use Unicode properties: ^\P{Lu}+$
\P means is not in Unicode category, and Lu is the uppercase letter that has a lowercase variant category.
^[a-z ]+$
Try this.This will validate the right ones.
It's not matching because you haven't used a space in the match pattern, so your regex is only matching whole words with no spaces.
try something like ^[a-z ]+$ instead (notice the space is the square brackets) you can also use \s which is shorthand for 'whitespace characters' but this can also include things like line feeds and carriage returns so just be aware.
This pattern does the following:
^ matches the start of a string
[a-z ]+ matches any a-z character or a space, where 1 or more exists.
$ matches the end of the string.
I would actually advise against regex in this case, since you don't seem to employ extended characters.
Instead try to test as following:
myString.equals(myString.toLowerCase());
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}