Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex does not match

Tags:

java

regex

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

like image 433
JuergenKie Avatar asked Aug 19 '13 13:08

JuergenKie


People also ask

How to match regex with string in Java?

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).

How do I not match a character in regex?

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]+/ .

What is matches() in Java?

Java String matches() The matches() method checks whether the string matches the given regular expression or not.

How do you use not regular expression?

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.


2 Answers

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.

Performance?

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.

UPDATE

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.

like image 195
Marko Topolnik Avatar answered Oct 01 '22 02:10

Marko Topolnik


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
like image 21
anubhava Avatar answered Oct 01 '22 02:10

anubhava