| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
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