Julio
Julio

Reputation: 1855

RegEx for finding two separate pieces of data in a block of text

Looking for a regular expression to find the words: David and 07888998 per line, They can be found more than once.

This is the data:

abcasdahadMichaeljkhdkjh 0888881SNADNA
SSMA,DAAASDDDavidjhsjdha007888998
asdsdASDDDavidjhsjdha==007888998asffafa
asdsdASDDDavidjhsjdha==007888995asffafa
SSMA|DAAASDDDaidjhsjdha007888998

The regular expression should find 2 matches. Line 2 and Line 3.

Any help is appreciated. Thanks

Upvotes: 1

Views: 114

Answers (4)

user557597
user557597

Reputation:

More ways:

^(?=.*David)(?=.*07888998)
or
(?:.*(?!\1)(David|07888998)){2}
or
(.*(?!\2)(David|07888998)){2}

Upvotes: 0

psyho
psyho

Reputation: 7212

This should work:

/(David.*07888998)|(07888998.*David)/

Upvotes: 0

codaddict
codaddict

Reputation: 455152

Since the order does not matter, you can use positive lookahead assertion (assuming the language/tool you are using supports it) as:

^(?=.*David)(?=.*07888998).*$

Rubular link

Upvotes: 3

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56390

If order matters, then /David.*07888998/ will find the matches you want.

If it doesn't matter and you want to make sure they both appear at least once, you can just "or" together two regexes that account for either order: /(David.*07888998|07888998.*David)/

Upvotes: 0

Related Questions