Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would you use a regular expression to ignore strings that contain a specific substring?
    text
    copied!<p>How would I go about using a negative lookbehind(or any other method) regular expression to ignore strings that contains a specific substring?</p> <p>I've read two previous stackoverflow questions:<br> <a href="https://stackoverflow.com/questions/367862/java-regexp-for-file-filtering">java-regexp-for-file-filtering</a><br> <a href="https://stackoverflow.com/questions/42990/regex-to-match-against-something-that-is-not-a-specific-substring">regex-to-match-against-something-that-is-not-a-specific-substring</a><br></p> <p>They are <em>nearly</em> what I want... my problem is the string doesn't end with what I want to ignore. If it did this would not be a problem.</p> <p>I have a feeling this has to do with the fact that lookarounds are zero-width and something is matching on the second pass through the string... but, I'm none too sure of the internals.</p> <p>Anyway, if anyone is willing to take the time and explain it I will greatly appreciate it.</p> <p>Here is an example of an input string that I want to ignore:</p> <p>192.168.1.10 - - [08/Feb/2009:16:33:54 -0800] "GET /FOO/BAR/ HTTP/1.1" 200 2246</p> <p>Here is an example of an input string that I want to keep for further evaluation:</p> <p>192.168.1.10 - - [08/Feb/2009:16:33:54 -0800] "GET /FOO/BAR/content.js HTTP/1.1" 200 2246</p> <p>The key for me is that I want to ignore any HTTP GET that is going after a document root default page.</p> <p>Following is my little test harness and the best RegEx I've come up with so far.</p> <pre><code>public static void main(String[] args){ String inString = "192.168.1.10 - - [08/Feb/2009:16:33:54 -0800] \"GET /FOO/BAR/ HTTP/1.1\" 200 2246"; //String inString = "192.168.1.10 - - [08/Feb/2009:16:33:54 -0800] \"GET /FOO/BAR/content.js HTTP/1.1\" 200 2246"; //String inString = "192.168.1.10 - - [08/Feb/2009:16:33:54 -0800] \"GET /FOO/BAR/content.js HTTP/"; // This works //String inString = "192.168.1.10 - - [08/Feb/2009:16:33:54 -0800] \"GET /FOO/BAR/ HTTP/"; // This works String inRegEx = "^.*(?:GET).*$(?&lt;!.?/ HTTP/)"; try { Pattern pattern = Pattern.compile(inRegEx); Matcher matcher = pattern.matcher(inString); if (matcher.find()) { System.out.printf("I found the text \"%s\" starting at " + "index %d and ending at index %d.%n", matcher.group(), matcher.start(), matcher.end()); } else { System.out.printf("No match found.%n"); } } catch (PatternSyntaxException pse) { System.out.println("Invalid RegEx: " + inRegEx); pse.printStackTrace(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload