hawx
hawx

Reputation: 1669

Start With and End With Regex

I have the rows listed below.

remove product 100 unit replace with 200
Remove Product 100 unit Replace 200
Remove 100 unit Replace with a 200
remove product 100 unit replace with an 200
remove product2 100 unit replace with an 200

I have attempted the regex below, with no luck.

/([remove 100|remove Product 100](.*?)[replace with 200|replace with a 200|replace with an 200])/i

It should flag the first four results, but not the last as its product2.

It flags everything on regexr. Example can be seen here: http://regexr.com/396u3

Any help would be appriciated.

Upvotes: 1

Views: 75

Answers (3)

Avinash Raj
Avinash Raj

Reputation: 174706

Your regex would be,

^remove (?:product ?)?100 (\S+) (?:replace (?:with )?(?:an |a )?200)$

This would capture only the string unit in the first 4 lines.

DEMO

Upvotes: 1

stema
stema

Reputation: 92986

Your problem is, that you are creating a character class

[remove 100|remove Product 100]

instead of an alternation

(remove 100|remove Product 100)

The character class matches only one character out of the character between the square brackets. So if you change your regex to

/((remove 100|remove Product 100)(.*?)(replace with 200|replace with a 200|replace with an 200))/i

it will match far better.

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80639

/remove (?:product )?100(.*?)replace (?:with )?(?:an? )?200/gi

The above expression is enough.

Upvotes: 1

Related Questions