-2

I need a simple regex that deletes all terms if it contains a comma.

Example:

input: cat dog duck output: cat dog duck

input: cat, dog, duck output: empty

  • 1
    What have you tried? What didn't work? What did you get? What did you expect? What doesn't work with your code and where is it? – Toto Jul 25 '20 at 15:49
  • nothing yet, I haven't found such a regex – Schalk Mátyás Jul 25 '20 at 16:45
  • Have a try with: find:`^\w+(?:, \w+)+$` replace:`NOTHING` – Toto Jul 25 '20 at 17:04
  • 1
    Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Jul 25 '20 at 18:01
  • yes, thanks it works, but only if the phrase consists of one word. if it consists of several words, it does not work. I forgot to mention this, sorry. so, `cat dog, cat dog duck, cat dog duck parrot,` etc. so to be precise, it should work with a multi-word list. – Schalk Mátyás Jul 25 '20 at 18:10

1 Answers1

0

You can use:

$tests = [
    'cat dog duck',
    'cat, dog, duck',
    'cat dog, cat dog duck, cat dog duck parrot',
];

foreach ($tests as $test) {
    echo preg_replace('/^[\w\h]+(?:, [\w\h]+)+$/', '', $test);
}

Output:

cat dog duck

Demo & explanation

Toto
  • 89,455
  • 62
  • 89
  • 125