0

I'm trying to match this regex

(sq.*f|gla|livingarea)

but I want it to not have the letters 'pr' anywhere in there.

so things like sqftpr or pricesqft etc dont' match.

I can prevent pricesqft with negative lookbehind

(?<!pr)(sq.*f|gla|livingarea)

I can't get the negative look behind to work with a negative look ahead. Any idea how I can get both?

(?<!pr)(sq.*f|g.?la|livingarea)(?!pr)

Any idea how I can block on both'pr' on both sides?

Chase R Lewis
  • 2,119
  • 1
  • 22
  • 47
  • Is it a hard limitation that you can call the regex-match function only once? If not, you could solve it quite easily by combining the existing regex call with a second one solely excluding the /pr/ matches. – Carsten Massmann Jul 27 '17 at 17:22
  • `sqftpr or pricesqft` are not matched by your regex, so your example is pretty bad :) guess you mean `sqprf` which shouldn't match the `pr` with `.*`... – dognose Jul 27 '17 at 17:34
  • Okay, understand... Take care with the wording "match", cause in Java (for example) `a match` does mean, that the whole string needs to MATCH the pattern - an implicit `^PATTERN$` so to say - What you mean, is java's `find`, hence `pricesqft` is **found** as `sqft`, which you like to avoid. – dognose Jul 27 '17 at 17:41
  • No I want javascript /{regex pattern}/.test() to evaluate to false for pricesqft and sqftprice but true for sqft or squarefeet – Chase R Lewis Jul 27 '17 at 17:49
  • That's the equal thing :) In java it's `match()` vs `find()` - in JS it's `match()` vs `test()` - so use `match()` rather than `test()` and you are good, except for the case where `pr` is covered by the `.*` of the pattern: `sqprf` - if that's a possible option at all. – dognose Jul 27 '17 at 17:50
  • ps.: syntax is `str.match(/pattern/)` rather than `/pattern/.test(str)` – dognose Jul 27 '17 at 17:53
  • @dognose I understand how regex is used ... but the pattern above doesn't work since it returns matches even when 'price' is in it. Without a proper pattern the above things won't work as intended within my code ... which is why I posted a question about having a proper matching pattern. – Chase R Lewis Jul 27 '17 at 17:56
  • Then please describe more detailed what you like to achieve. Is the string you want to `find` inside other strings like `I have 258 squarefeet of space` or is the *value you check against* already cut down to a clear start/end? `258 squarefeet`. If you want to extract information from the first case, you need lookarounds. If you just want to validate the second-case, your pattern along with `match()` rather than `test()` would be enough. – dognose Jul 27 '17 at 17:59
  • 1
    You should include Sample input, and desired output examples. (What should and shouldn't match) – Tezra Jul 27 '17 at 18:06
  • I was able to find it throwing stuff around for another 30 min posted my answer below. – Chase R Lewis Jul 27 '17 at 18:50

1 Answers1

0

(?=^((?!pr).)$).(?=(sq.*f|g.?la|livingarea)) This appears to be the answer, a positive lookahead (essentially an and) that looks for 'pr' and nots it with what I want to match to the right.

Chase R Lewis
  • 2,119
  • 1
  • 22
  • 47