Java regular expression match end doesn't work [duplicate] - java

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 2 years ago.
I want to match the last character "c". What am I doing wrong? In the documentation it's clearly explained that $ matches the end of the line, i have used regex milions of time on unix shell ecc... and always worked as expected but in java no.
String string = "abc";
if(string.matches("c$")){//I know that .*c$ will work.
System.out.println("yes");//This is never printed
}
Where is the error?
I know that .*c$ will work, but by reading the javadoc I can't find this information.
Can some one tell me how do I interpret what is the meaning of this java official tutorial?
https://docs.oracle.com/javase/tutorial/essential/regex/bounds.html
or this?
https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum
Am I not able to read? Because it seems pretty obvious, but I really can't find the solution, I fell really retarded in this moment!
Under java.lang.String there is a method who clearly say the following:
matches(String regex)
Tells whether or not this string matches the given regular expression.

This will print YES.
String line = "abc";
Pattern pattern = Pattern.compile("c$");
Matcher matcher = pattern.matcher(line);
System.out.println(matcher.find() ? "YES" : "NO");

Related

Regular Expression (regex) fails even though the express matches the provided string and best/efficient way [duplicate]

This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 2 years ago.
I am trying to implement custom regular expressions in my Java program to verify if my input string matches the regex. But for some reason, the regex fails at a particular point even though the input string matches it.
String input = "abc:def:id:identifier:123456.890123.1";
System.out.println(input.matches("(abc:def:id:identifier:).[0-9]{6,12}.[0-9]{1,6}.[0-9]{1,20}"));
The regex returns false at the point 123456 even though the input matches the length of 6.
If I give 1234567 then it would return true. I am wondering why it fails for 123456 even though it matches the length. Also, it does not fail for the next one where I have just 1 and length of {1,20}.
Also, is there a better way to verify this string in regex or this is a good and efficient way?
As mentioned by #JvdV, should remove . at the start which is causing the problem during the validation.
Following should work:
input.matches("(abc:def:id:identifier:)[0-9]{6,12}\\.[0-9]{1,6}\\.[0-9]{1,20}")

Regex will quite if the first character does not match the pattern, even if there should be a match later [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 3 years ago.
I am using regex in java, and I cannot create a regex to match what I want it to. I want to match everything in a string that begins and ends with a character.
"cats-are-cute" should match and return cats-are-cute
!!!DOG-CAT!!! should match and return DOG-CAT
I am using https://regexr.com/ to test, and it says my regex should work
I'm not even sure how I should attempt to fix this. I've found out that it will quite if the very first character does not match (e.i it is a special character) but it will match if the entire string begins + ends with a matching character.
It will not match if a special character begins or ends the entire string
Here is my code:
Pattern pattern = Pattern.compile("([A-Za-z0-9].*[A-Za-z0-9])");
Matcher matcher = pattern.matcher(word);
if(matcher.matches())
{
System.out.println("Matches");
System.out.println(matcher.start());
System.out.println(matcher.end());
}
if I type
testing
it returns
Matches
0
7
Small question: why is it 7 and not 6?
just like it should
but if I do "testing" matcher.matches() is false.
I think it should output
Matches
1
7
but sadly it does not as matcher.matches() returns false.
I think my regex is working, because quite a few sites have said that my regex will match what I want it to.
Am I missing something with Matcher matches()? Does it not do what I think it does?
I just needed to use find instead of matches, as OH GOD SPIDERS suggested in this comment:
As the documentation of Matcher.matches states it Attempts to match the entire region against the pattern.. You need to use Matcher.find if you don't want your entire String to be matched.

Regular expression with captured groups for parsing a range of two real numbers [duplicate]

This question already has answers here:
simple java regex throwing illegalstateexception [duplicate]
(3 answers)
Closed 5 years ago.
I need a regex to properly parse ranges of two real numbers (unsigned), presented with a hyphen.
Valid inputs:
1-3
3.14-7.50
0-4.01
It's Java on Android.
My current approach:
Pattern pattern = Pattern.compile("(?<Minimum>\\d+(\\.\\d+))-(?<Maximum>\\d+(\\.\\d+))");
Matcher matcher = pattern.matcher("3.14-5.2");
String min = matcher.group("Minimum");
String max = matcher.group("Maximum");
It crashes on attempting to retrieve the minimum.
java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.getMatchedGroupIndex(Matcher.java:1314)
at java.util.regex.Matcher.group(Matcher.java:572)
I can't really see what's wrong with the expression.
I would particularly appreciate an explanation on what the problem with it is. A regex allowing for optional white space around the hyphen would be extra nice, too (I'd like it to work that way but I dropped this for now as I can't get it to work at all).
You need to make decimal part optional:
Pattern pattern = Pattern.compile(
"(?<Minimum>\\d+(?:\\.\\d+)?)-(?<Maximum>\\d+(?:\\.\\d+)?)");
? after (?:\\.\\d+) will make that group an optional match
Better to use ?: for making it a non-capturing group
Also you need to call matcher.find() or matcher.matches() before calling .group(int) method.

Why I must specify whole string in Java regular expression? [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 6 years ago.
Suppose, I have a string:
String str = "some strange string with searched symbol";
And I want to search in it some symbols, suppose it will be "string". So we have a following:
str.matches("string"); //false
str.matches(".*string.*"); //true
So, as stated in the title, why I must specify whole string in Java regular expression?
Java documentation says:
public boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
It doesn't says
Tells whether or not this whole string matches the given regular expression.
For example, in the php it would be:
$str = "some strange string with searched symbol";
var_dump(preg_match('/string/', $str)); // int(1)
var_dump(preg_match('/.*string.*/', $str)); //int(1)
So, both of the regex's will be true.
And I think this is correct, because if I want to test whole string I would do str.matches("^string$");
PS: Yes, I know that is to search a substring, simpler and faster will be to use str.indexOf("string") or str.contains("string"). My question regards only to Java regular expression.
UPDATE: As stated by #ChrisJester-Young (and #GyroGearless) one of the solutions, if you want to search regex that is part of a subject string, is to use find() method like this:
String str = "some strange string with searched symbol";
Matcher m = Pattern.compile("string").matcher(str);
System.out.println(m.find()); //true
matches always matches the whole input string. If you want to allow substrings to match, use find.
As the documentation you suggest,
public boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
What it means is whether that string matches with the given regex. i.e. matches verifies whether your string is an instance of the given regex.
not whether it contains a substring which is an instance of the given regex. As Chris suggested you can use find instead or for your problem you can use contains.
Yes, as you already know (now) that matches() will return true only for the complete string.
But, in your update, you have stated that:
As stated by #ChrisJester-Young (and #GyroGearless) the only solution, if you want to search regex that is part of a subject string, is to use find() method...
I would like to say that using find() is not the only solution. There is at least one more, which I know :
String str = "some strange string with searched symbol";
boolean found = str.split("string").length>1;
System.out.println(found);
This prints true. And this will work for all regular expressions. Though this is not the way to do it, and is instead a hack.
There may be many more solutions.

A pattern that matches all except starting with a word [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 2 years ago.
How can I set a regex pattern which matches all words but the strings which starts with
/word
/word/
/word/ following by anything else.
I think the pattern starts with \A but I don'0t know how to tell that should not follow a word
Thanks
Use this kind of negate regex and replace word by your word.
^((?!word).)*$
You can use the regex:
^(?!\\/word).*$
See it
Take a look at the lookaround feature offered by regular expressions. Also, a similar thread. Also, posting your question in terms of a concrete sample problem might help you get a few working sample snippets.
Perhaps using a string comparison will be clearer and faster.
if (text.startsWith("word")) {
// text is OK
} else {
// not OK
}

Categories