16

I am looking for a regex that will match a string that starts with one substring and does not end with a certain substring.

Example:

// Updated to be correct, thanks @Apocalisp
^foo.*(?<!bar)$

Should match anything that starts with "foo" and doesn't end with "bar". I know about the [^...] syntax, but I can't find anything that will do that for a string instead of single characters.

I am specifically trying to do this for Java's regex, but I've run into this before so answers for other regex engines would be great too.

Thanks to @Kibbee for verifying that this works in C# as well.

1
  • As I mentioned below - regexp in question for input string "foo123bar" it will match "foo12" - hope this is desired behavior.
    – Dmitry
    Dec 23, 2015 at 22:44

4 Answers 4

9

I think in this case you want negative lookbehind, like so:

foo.*(?<!bar)
1

Verified @Apocalisp's answer using:

import java.util.regex.Pattern;
public class Test {
  public static void main(String[] args) {
    Pattern p = Pattern.compile("^foo.*(?<!bar)$");
    System.out.println(p.matcher("foobar").matches());
    System.out.println(p.matcher("fooBLAHbar").matches());
    System.out.println(p.matcher("1foo").matches());
    System.out.println(p.matcher("fooBLAH-ar").matches());
    System.out.println(p.matcher("foo").matches());
    System.out.println(p.matcher("foobaz").matches());
  }
}

This output the the right answers:

false
false
false
true
true
true
1

I'm not familiar with Java regex but documentation for the Pattern Class would suggest you could use (?!X) for a non-capturing zero-width negative lookahead (it looks for something that is not X at that postision, without capturing it as a backreference). So you could do:

foo.*(?!bar) // not correct

Update: Apocalisp's right, you want negative lookbehind. (you're checking that what the .* matches doesn't end with bar)

1
  • Problem with this solution is in the fact that for input string "foo123bar" it will match "foo12" - hope this is desired behavior.
    – Dmitry
    Dec 23, 2015 at 22:43
0

As other commenters said, you need a negative lookahead. In Java you can use this pattern:

"^first_string(?!.?second_string)\\z"
  • ^ - ensures that string starts with first_string
  • \z - ensures that string ends with second_string
  • (?!.?second_string) - means that first_string can't be followed by second_string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.