5

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

4
  • And what specific misbehavior are you getting? (Also, is your actual scenario more complicated than what you posted? If not, why aren't you just using line.charAt(0)?) Commented Aug 19, 2013 at 13:01
  • 2
    M? means "0 or 1 M characters", which will always be true. Commented Aug 19, 2013 at 13:01
  • what i want is the path at the end of the line. That's why i worked with () Commented Aug 19, 2013 at 13:20
  • In this case you need to first find where the path begins. Basically, it's a new question now. Commented Aug 19, 2013 at 13:33

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Performance? : Even if you use static Pattern myPattern = Pattern.compile(regex); for heavy match processes?
Finally, i want the path at the end of the line. This is why i used () Groups and startsWith() doesn't fit my Needs.
Did you test the code? If i run it with the input i've posted above, i get no matches at all. It matches when i replace the S with s, but then the Group doesn't contain the path
3

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

Comments

3

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

4 Comments

The regex is wrong: it will match only the literal line "M". Also, no need for begin/end anchors; that's implied by the semantics of matches.
line.matches("^M") will fail if line has any character after M.
And charAt(0) will throw an unwanted exception for empty string.
@MarkoTopolnik Thanks, I haven't written any Java in quite a while, so I'm a bit rusty.
0

Why not do this

line.startsWith("M");

Comments

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

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

2 Comments

Finally, i want the path at the end of the line. This is why startsWith() doesn't fit my Needs.
@der_juergen Add an EDIT to your post and complete it with the new answer (do not delete first lines, new readers will understand the thread)
0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.