0

I'm struggling to find the correct RegEx for this problem:

I want to search a string for a value. If true, the result should be an emty string, otherwise the whole string.

Example

"We have apples, oranges, and peas"

Check for "apples" -> "

Check for "tomato" -> "We have apples, oranges, and peas"

I have to solve it using a standard RegEx statement. Since it is the only way I can modify the original string in the application.

Basically what I want to do is:

s="We have apples, oranges, and peas"

w="tomato"

IF instr(s,w)>0 then result="" else result=s

The problem is that the only way I can modify the string is by a regex syntax

I believe I have solved it by myself. Have no clue why it works :)

 (?(?=.*apples)|.*)|.*

returns nothing as I want

 (?(?=.*banana)|.*)|.*

returns We have apples, oranges, and peas

The following regex also works

 ^((?!apples).)*$
  • 2
    Which tool/language? – Tushar Jan 22 '16 at 12:06
  • How is this a duplicate? – ndnenkov Jan 22 '16 at 12:12
  • The string `We have apples, oranges, and please` does not contain `tomato`, and OP wants to get it. Thus, the question is a dupe. – Wiktor Stribiżew Jan 22 '16 at 17:36
  • @WiktorStribiżew, and he wants to match an empty string if there is a `tomato`, not return `null`/no match or whatever the default behavior in the language is. Thus the question is different. – ndnenkov Jan 22 '16 at 17:38
  • I disagree. And don't worry: SO is a democratic site: if others agree it is so different, then the question will be reopened. – Wiktor Stribiżew Jan 22 '16 at 17:39
  • @WiktorStribiżew, if others cared to check the question out... you can be pretty sure max 1-2 people looked at the question after you closed it, of them probably noone had enough reputation to cast reopen votes. – ndnenkov Jan 22 '16 at 17:41

0 Answers0