0

If [\s\d]* means: repeat matching space-character or digit. What would be the syntax for ignoring the entire line if there suddenly is a "-" character?

Like in this example https://regex101.com/r/eP1kG1/1 The last line should not match!

.*\bAPPLE.*\bPIE[\s\d]*

SWEET APPLE 1 - PIE 2 SWEET APPLE 1 - PIE 2 IS GOOD SWEET APPLE 1 - PIE3 SWEET APPLE 1 - PIE 1-3 IS GOOD

Only the last line should not match, meaning: When there is a "-" after PIE it should fail.

Also this is for sql, and not possible to use look-ahead/behind

Baked Inhalf
  • 3,375
  • 1
  • 31
  • 45
  • What have you tried so far? By the way, we should not have to leave SO to see what you mean. – Jonast92 Apr 21 '15 at 21:19
  • $, [^-] even groups, but it fails since [^-] means a character but not a "-" – Baked Inhalf Apr 21 '15 at 21:20
  • your question is not clear. please state what your your problem is, sample input and expected outputs. it is also important in which context do you intend to use it. – 1010 Apr 21 '15 at 21:20
  • possible duplicate of [A regex to match a substring that isn't followed by a certain other substring](http://stackoverflow.com/questions/2631010/a-regex-to-match-a-substring-that-isnt-followed-by-a-certain-other-substring) – Alex A. Apr 21 '15 at 21:21
  • @stribizhev That line is fine. It's just the last one that is incorrect – Baked Inhalf Apr 21 '15 at 21:22
  • _this is for sql_ - What DBMS are you using as the answer could vary. And also if this is for a Select statement why not specify it in the where clause. – Java Devil Apr 21 '15 at 21:30
  • @JavaDevil Oracle sql developer. I'm using the regexp_like method in a where clause – Baked Inhalf Apr 21 '15 at 21:32
  • 1
    You can try another approach: disallow `-` up to the string end: `^.*\bAPPLE.*\bPIE[\s\d]*[^-]*$`. See https://regex101.com/r/eP1kG1/3. Look-arounds are "expensive". – Wiktor Stribiżew Apr 21 '15 at 21:35
  • @stribizhev Nice one! You should post that one as an answer! :) – Baked Inhalf Apr 21 '15 at 21:38

2 Answers2

1

Is this what you want? Just checking to make sure PIE[\s\d]* isn't followed by a - on the line:

(?!.*PIE[\s\d]*-).*\bAPPLE.*\bPIE[\s\d]*
tenub
  • 3,386
  • 1
  • 16
  • 25
  • Exactly, just need to test this in sql. Maybe my [\s\d]* isn't the best way because there can be other valid characters after, like in line two.... – Baked Inhalf Apr 21 '15 at 21:30
1

As I mentioned in my comment, look-aheads usually consume much more computing power. I'd suggest the following regex:

^.*\bAPPLE.*\bPIE[\s\d]*[^-]*$

It just requires all characters after [\s\d]* to be non-hyphens. Anchors - ^ and $ - make sure we only check the whole string.

See demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563