Allan Karlson
Allan Karlson

Reputation: 499

php regex combine repeated list with single occurrence and exclude whole words

I fail with some regex task I need for replacing some text information.

The following task can be done also in not regex but if would be nice to have it in regex because there is much to replace.

However the question is:

So if I use: ((?!for)[\w\. \&\=\?\-\(\)\'\+]{3,99}|(\n?))<test@mail\.se> the word 'for' will be matched even though. Do anyone has an idea?

So example: Within this string:

    To: Lasse Erikson <[email protected]>

I want to match the name and the mail.

Even here:

To: Lasse Erikson <[email protected]>, Sara
    Larsson <[email protected]>

But here I want not fetch anything:

 for <[email protected]>; Thu, 14 Dec 2017 21:18:22 +0100 (CET)

Because there is a "for" in the line.

I hope you understand this...

Thank you in advance

Upvotes: 2

Views: 67

Answers (1)

revo
revo

Reputation: 48711

PHP (PCRE) provides some verbs to skip over or fail current matching process that you can use to skip over word for or fail whole match if you want:

((?(?=\bfor\b)for(*SKIP)(*F))[ \w.(&=?\-+)](?(2)|(\R)?)){3,99}<[^<>@]*@[^<>]*>

Live demo

Breakdown:

(   # Start of capturing group #1
    (?(?=\bfor\b)for(*SKIP)(*F))    # Skip over `for` if any
    [ \w.(&=?\-+)]  # Match whitelist characters
    (?(2)|(\R)?)    # One newline character between
){3,99} # Between 3 or 99 times, end of CG #1
<[^<>@]*@[^<>]*> # Match an email format

Upvotes: 1

Related Questions