# Regular expressions "bypassing" conditions

**URL:** <https://discourse.processing.org/t/regular-expressions-bypassing-conditions/37613>\
**Category:** Coding Questions\
**Created:** [June 18, 2022, 9:23am UTC](https://discourse.processing.org/t/regular-expressions-bypassing-conditions/37613 "2022-06-18T09:23:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [June 18, 2022, 9:23am UTC](https://discourse.processing.org/t/regular-expressions-bypassing-conditions/37613/1 "2022-06-18T09:23:37Z")

</div>

I’m currently experementing with regular expressions. And am currently writing code that is supposed to take a String and find all points where a string is defined in code like “test” or ““this text is in quotes””.  
I set the following rules:  
It has to begin with a " without a \ coming before  
Between the begin and the end there should be a minimal amount of other chars  
It has to end with a " without a \ coming before

I made the following regex `"(?!\\\\)\".*?(?!\\\\)\""`  
However my code seems to ignore the end condition. I think this is because if it ends with " it re-interprets the \ as a part of the .\*? part.

Here is the code I’m currently using.

```auto
String test="\"test1\\\"\" test2 \"test 3\"";
import java.util.regex.*;

void setup() {
  println(find(test, "(?!\\\\)\".*?(?!\\\\)\""));
}
int[] find(String inp, String regex) {
  Pattern p=Pattern.compile(regex);
  Matcher matcher=p.matcher(" "+inp+" ");

  ArrayList<Integer> matchesBeg=new ArrayList();
  if (matcher.find()) do {
    matchesBeg.add(matcher.start()) ;
    println((" "+inp+" ").substring(matcher.start(),matcher.end()));
  } while (matcher.find(matcher.end()+1));
  int ret[]=new int[matchesBeg.size()];
  for (int i=0; i<ret.length; i++) ret[i]=matchesBeg.get(i);
  return ret;
}

```

And this is a minimal example in wich the problem occurs:

```auto
String sample="\"\\\"\"";
String regex="(?!\\\\)\".*?(?!\\\\)\"";
println(sample+"\n"+regex);
println(sample.matches(regex));

```

Does someone know how to fix the regex?

(Edit: Ok I managed to fix it myself. The correct regex is: `String regex="(?<!\\\\).*(?<!\\\\)\""`.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 18, 2022, 11:39am UTC](https://discourse.processing.org/t/regular-expressions-bypassing-conditions/37613/2 "2022-06-18T11:39:31Z")

</div>

Hi @NumericPrime,

Info [negative-lookbehind](https://www.baeldung.com/java-regex-lookahead-lookbehind#negative-lookbehind)

[check here if it will be sufficient for your needs…](https://regex101.com/r/PVd18C/1)

Cheers  
— mnse

PS: Ups! Missed your edit 😀
