I know that this kind of questions are proposed very often, but I can't figure out why this RegEx does not match. I want to check if there is a "M" at the beginning of the line, or not. Finaly, i want the path at the end of the line. This is why startsWith() doesn't fit my Needs.
line = "M 72208 70779 koj src\com\company\testproject\TestDomainf1.java";
if (line.matches("^(M?)(.*)$")) {}
I've also tried the other way out:
Pattern p = Pattern.compile("(M?)");
Matcher m = datePatt.matcher(line);
if (m.matches()) {
System.out.println("yay!");
}
if (line.matches("(M?)(.*)")) {}
Thanks
Java - String matches() Method This method tells whether or not this string matches the given regular expression. An invocation of this method of the form str. matches(regex) yields exactly the same result as the expression Pattern. matches(regex, str).
To replace or remove characters that don't match a regex, call the replace() method on the string passing it a regular expression that uses the caret ^ symbol, e.g. /[^a-z]+/ .
Java String matches() The matches() method checks whether the string matches the given regular expression or not.
NOT REGEXP in MySQL is a negation of the REGEXP operator used for pattern matching. It compares the given pattern in the input string and returns the result, which does not match the patterns. If this operator finds a match, the result is 0. Otherwise, the result is 1.
The correct regex would be simply
line.matches("M.*")
since the matches
method enforces that the whole input sequence must match. However, this is such a simple problem that I wonder if you really need a regex for it. A plain
line.startsWith("M")
or
line.length() > 0 && line.charAt(0) == 'M'
or even just
line.indexOf('M') == 0
will work for your requirement.
If you are also interested in performance, my second and third options win in that department, whereas the first one may easily be the slowest option: it must first compile the regex, then evaluate it. indexOf
has the problem that its worst case is scanning the whole string.
In the meantime you have completely restated your question and made it clear that the regex is what you really need. In this case the following should work:
Matcher m = Pattern.compile("M.*?(\\S+)").matcher(input);
System.out.println(m.matches()? m.group(1) : "no match");
Note, this only works if the path doesn't contain spaces. If it does, then the problem is much harder.
You dont need a regex for that. Just use String#startsWith(String)
if (line.startsWith("M")) {
// code here
}
OR else use String#toCharArray()
:
if (line.length() > 0 && line.toCharArray()[0] == 'M') {
// code here
}
EDIT: After your edited requirement to get path from input string.
You still can avoid regex and have your code like this:
String path="";
if (line.startsWith("M"))
path = line.substring(line.lastIndexOf(' ')+1);
System.out.println(path);
OUTPUT:
src\com\company\testproject\TestDomainf1.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With