JuergenKie
JuergenKie

Reputation: 109

Java Regex does not match

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

Upvotes: 5

Views: 3200

Answers (6)

anubhava
anubhava

Reputation: 785146

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

Upvotes: 3

cl-r
cl-r

Reputation: 1264

If you need Path, you can split (I guess than \t is the separator) the string and take the latest field:

String[] tabS = "M            72208    70779 kij          src\com\knapp\testproject\TestDomainf1.java".split("\t");
String path = tabS[tabS.length-1];

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200158

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.

Upvotes: 5

p.s.w.g
p.s.w.g

Reputation: 149020

You can use this pattern to check whether an M character appears as at the beginning of the string:

if (line.matches("M.*"))

But for something this simple, you can just use this:

if (line.length() > 0 && line.charAt(0) == 'M')

Upvotes: 3

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

   String str = new String("M 72208 70779 kij src/com/knapp/testproject/TestDomainf1.java");

   if(str.startsWith("M") ){
        ------------------------
        ------------------------
   }

Upvotes: 0

Anirudha
Anirudha

Reputation: 32797

Why not do this

line.startsWith("M");

Upvotes: 0

Related Questions