Help with samples of regular expressions syntax please

Help with samples of regular expressions syntax please

Post by Larry Bad » Fri, 19 Feb 1999 04:00:00

What  regular expressions syntax would match:


> 1) Lines that contain the words "hot" and "cold"

> syntax= ?

> 2) Lines that contain the words "eat" but not "cats"

> syntax= ?


Help with samples of regular expressions syntax please

Post by era eriksso » Sat, 20 Feb 1999 04:00:00

(Larry Bados) posted to comp.os.linux.misc,comp.unix.user-friendly:

When posting to multiple newsgroups, please set up a followup-to to
just one group. I have set followups for this to c.u.u-f (not that I
expect any).

It is also generally considered inappropriate to ask for help with
your homework on Usenet.

Hot tip: Read the stuff in news.announce.newusers before posting

 > What  regular expressions syntax would match:
 >> 1) Lines that contain the words "hot" and "cold"
 >> 2) Lines that contain the words "eat" but not "cats"

 1) hot|cold -- note that if you test this with egrep, you need to
    quote the regular exression. Actually it's a good idea to +always+
    single-quote the first argument to grep. (You get in trouble when
    you want to search for a single quote character, but let's discuss
    this in the advanced shell programming course.)

    Plain Old Grep didn't have the | alternation operator, although
    modern implementations usually understand it, perhaps in the form
    \| -- traditionally, this is the domain of "extended regular
    expressions" and thus egrep.

    If you need to do this strictly with a traditional "unextended"
    expression, it's not directly possible, but you can of course set
    up a pipeline (see next answer). Some greps will actually let you
    use the following -- I believe undocumented -- syntax:

        grep -e 'hot' -e 'cold' files

    (Don't tell them I told you this.)

 2) is not very easy to express with standard regular expressions. You
    can set up a pipeline something like

        grep 'eat' files | grep -v 'cats'

    The purpose of the exercise is probably to trick you into thinking
    that eat[^s] or [^c]at[^s] or even -- good heavens, close your
    eyes when reading this -- [e^c]at[^s] or [^cats] is a solution,
    and then humiliate you in class enough to make you think about why
    it's not a solution after all.

    Actually you could construct a regular expression something like
    this (I had to split it over several lines so I thought I might as
    well comment it. Remove the comments (# whatever), all the
    surrouding whitespace, and glue this back together):

      ^                                    # beginning of line
      (                                    # group the following:
        ([^ec]|[ec][^a]|[ec]a[^t]|cat[^s])*  # any number of non-eat non-cats
        eat                                  # eat
      )+                               # at least one occurrence of this group
      ([^ec]|[ec][^a]|[ec]a[^t]|cat[^s])*  # more optional non-eat non-cats
      (c(at?)?)?                           # permit a partial cat before ...
      $                                    # ... end of line.

    This is again something Plain Old Grep won't cope with. The
    parentheses (for grouping), the ? "zero or one" operator, and the
    | alternation operator are not standard in grep. Even the + "one
    or more" operator was, I believe, not known to the original grep.
    But like I wrote above, many modern greps have some or all of
    these operators in some form. Anyhow, with egrep you'll be safe
    and portable.

In general, if your teacher has not provided you with the necessary
materials to figure this out yourself, you should complain to his/her
superiors. Meanwhile, any old introduction to Unix will cover regular
expressions (and glob patterns, which you asked about earlier) in some

Hope this helps,

/* era */

.obBotBait: It shouldn't even matter whether    <>
I am a resident of the state of Washington. <>


1. Regular Expression Syntax Limitation?

Hi all,

I've been trying to use grep to filter a file whenever it says Copyright
[some name other than Microsoft].

I thought this expression syntax might be appropriate:


However this still finds strings that are Copyright ... Microsoft (e.g.
Copyright (c) 1997 - 1999 Microsoft Corporation). This makes sense
because, for example, " (c) 1997" doesn't match "Microsoft".

Any advice about how to approach this situation differently?

Many thanks,

2. DOS and LINUX on same machine?

3. Need help with a regular expression please.

4. Linux on IBM RS/6000 43P - Help!

5. need help with a regular expression

6. "duplicate const" compile warning in 2.5.42

7. Regular Expression help

8. access floppy drive

9. Sed and regular expression help

10. HELP: regular expressions

11. HELP: Regular Expression Needed

12. Help with regular expressions

13. Sed and regular expression help