| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<89CB002449C1A54BA3FCB5656E0A68290C8AC9D7@CH2WPEXCH3.na.ds.ussco.com> |
For straight unix, you can use
grep color input.txt | grep -e red -e blue
Jim
-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: Wednesday, April 08, 2015 10:39 AM
To: [email protected]
Subject: Re: RegEx: find words in random order
On 2015-04-08 16:49, [email protected] [sed-users] wrote:
> Sorry, I forgot to mention, that I asked here, because here are RegEx
> specialists. The RegEx is for another program.
>
> > which would first filter for lines containing "color" and then, if
> > "red\|blue" is found on the same line, print it.
>
> Thank you very much, maybe you could help anyway with a
> not-sed-solution.
No sweat. It would still help to know what you want to do with them, and would also help to know the dialect of regex/language you're using. For the case you describe, I'd just do two matches. E.g., in python, either
for line in file("input.txt"):
if "color" in line and any(s in line for s in ["red", "blue"]):
do_something(line)
You also have the odd edge-case where a line might contain the word "colored" which does have both "color" and "red" in it, but isn't likely what you want. If you really do need regex power or want to prevent that edge-case, you can use something like:
import re
r = re.compile(r"(?=.*\bcolor\b)(?=.*\b(?:red|blue)\b)")
for line in file("input.txt"):
if r.match(line):
do_something(line)
But as noted, the particulars vary based on your flavor of regex.
-tim
------------------------------------
Posted by: Tim Chase <[email protected]>
------------------------------------
--
------------------------------------
Yahoo Groups Links
**********************************************************************
Information contained in this e-mail message and in any attachments thereto is confidential. If you are not the intended recipient, please destroy this message, delete any copies held on your systems, notify the sender immediately, and refrain from using or disclosing all or any part of its content to any other person.