Denial literal strings in Java regex

So regular expressions seem to match in the longest match. For instance:

public static void main(String[] args) { String s = "ClarkRalphKentGuyGreenGardnerClarkSupermanKent"; Pattern p = Pattern.compile("Clark.*Kent", Pattern.CASE_INSENSITIVE); Matcher myMatcher = p.matcher(s); int i = 1; while (myMatcher.find()) { System.out.println(i++ + ". " + myMatcher.group()); } } 

generates output

  • ClarkRalphKentGuyGreenGardnerClarkSupermanKent

I need this conclusion

  • Clarkralphkent
  • ClarkSupermanKent

I tried templates like:

  Pattern p = Pattern.compile("Clark[^((Kent)*)]Kent", Pattern.CASE_INSENSITIVE); 

which do not work, but you see what I'm trying to say. I want the line from Clark to Kent not to contain Kent.

This line:

ClarkRalphKentGuyGreenGardnerBruceBatmanKent

should generate output

  • Clarkralphkent
+4
source share
4 answers

greedy against reluctant - your friend is here.

try: Clark.+?Kent

+6
source

You need a "reluctant", not a "greedy" quantifier. Just put it? after your * has to do the trick.

+4
source

When you tried "Clark[^((Kent)*)]Kent" , I think you wanted "Clark((?!Kent).)*Kent" for a negative forecast with zero width (scroll down the bit to the header) Look-Around Assertions ").

In parentheses indicate the correspondence of characters or pattern matching. So, RegExp was trying to find one character not in (, K, e, n, t, ), * .

+3
source

Use the relunctant suffix ? : Clark.*?Kent Quantum ? , * , + can follow ? to indicate that they should stop as soon as possible.

cm. http://perldoc.perl.org/perlre.html

+2
source

All Articles