Regular expression to match a #

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Deco

    #1

    Regular expression to match a #

    Hi,

    I'm trying to use a regular expression to match a string containing a #
    (basically i'm looking for #include ...)

    I don't seem to manage to write a regular expression that matches this.

    My (probably to naive) approach is: p = re.compile(r'\b #include\b)
    I also tried p = re.compile(r'\b \#include\b) in a futile attempt to use
    a backslash as escape character before the #
    None of the above return a match for a string like "#include <stdio>".

    I know a # is used for comments, hence my attempt to escape it...

    Any suggestion on how to get a regular expression to find a #?

    Thanks

  • Dan

    #2
    Re: Regular expression to match a #

    > My (probably to naive) approach is: p = re.compile(r'\b #include\b)

    I think your problem is the \b at the beginning. \b matches a word break
    (defined as \w\W or \W\w). There would only be a word break before the #
    if the preceding character were a \w (that is, [A-Za-z0-9_], and maybe
    some other characters depending on your locale).

    However, the \b after the "include" is exactly what you want.

    --
    I had picked out the theme of the baby's room and done other
    things. I decided to let Jon have this.
    - Jamie Cusack (of the Netherlands), whose husband Jon
    finally talked her into letting him name their son Jon 2.0


    Comment

    • Tom Deco

      #3
      Re: Regular expression to match a #

      Thanks,
      That did the trick...

      Comment

      • Duncan Booth

        #4
        Re: Regular expression to match a #

        Dan wrote:
        [color=blue][color=green]
        >> My (probably to naive) approach is: p = re.compile(r'\b #include\b)[/color]
        >
        > I think your problem is the \b at the beginning. \b matches a word break
        > (defined as \w\W or \W\w). There would only be a word break before the #
        > if the preceding character were a \w (that is, [A-Za-z0-9_], and maybe
        > some other characters depending on your locale).
        >
        > However, the \b after the "include" is exactly what you want.
        >[/color]

        So the OP probably wanted '\B' the exact opposite of '\b' for the start of
        the string, i.e. only match the # if it is NOT preceded by a wordbreak.

        Alternatively for C style #includes search for r'^\s*#\s*inclu de\b'.

        Comment

        • John Machin

          #5
          Re: Regular expression to match a #

          Tom Deco wrote:[color=blue]
          > Hi,
          >
          > I'm trying to use a regular expression to match a string containing a #
          > (basically i'm looking for #include ...)
          >
          > I don't seem to manage to write a regular expression that matches this.
          >
          > My (probably to naive) approach is: p = re.compile(r'\b #include\b)
          > I also tried p = re.compile(r'\b \#include\b) in a futile attempt to use
          > a backslash as escape character before the #
          > None of the above return a match for a string like "#include <stdio>".
          >
          > I know a # is used for comments, hence my attempt to escape it...
          >
          > Any suggestion on how to get a regular expression to find a #?
          >
          > Thanks
          >[/color]

          You definitely shouldn't have the first \b -- match() works only at the
          beginning of the target string, so it is impossible for there to be a
          word boundary just before the "#".

          You probably shouldn't have the second \b.

          You probably should read section A12 of K&R2.

          You probably should be using a parser, but if you persist in using
          regular expressions:

          (a) read the manual.

          (b) try something like this:
          [color=blue][color=green][color=darkred]
          >>> pat1 = re.compile(r'\s *#\s*include\s* <\s*([^>\s]+)\s*>\s*$')
          >>> pat1.match(" # include < fubar.h > ").group(1)[/color][/color][/color]
          'fubar.h'

          N.B. this is based the assumption that sane programmers don't have
          whitespace embedded in the names of source files ;-)

          HTH,
          John

          Comment

          • John Machin

            #6
            Re: Regular expression to match a #

            Duncan Booth wrote:[color=blue]
            > Dan wrote:
            >
            >[color=green][color=darkred]
            >>>My (probably to naive) approach is: p = re.compile(r'\b #include\b)[/color]
            >>
            >>I think your problem is the \b at the beginning. \b matches a word break
            >>(defined as \w\W or \W\w). There would only be a word break before the #
            >>if the preceding character were a \w (that is, [A-Za-z0-9_], and maybe
            >>some other characters depending on your locale).
            >>
            >>However, the \b after the "include" is exactly what you want.
            >>[/color]
            >
            >
            > So the OP probably wanted '\B' the exact opposite of '\b' for the start of
            > the string, i.e. only match the # if it is NOT preceded by a wordbreak.
            >
            > Alternatively for C style #includes search for r'^\s*#\s*inclu de\b'.[/color]

            Search for r'^something' can never be better/faster than match for
            r'something', and with a dopey implementation of search [which Python's
            re is NOT] it could be much worse. So please don't tell newbies to
            search for r'^something'.

            Comment

            • Jeff Schwab

              #7
              Re: Regular expression to match a #

              John Machin wrote:
              [color=blue]
              > Search for r'^something' can never be better/faster than match for
              > r'something', and with a dopey implementation of search [which Python's
              > re is NOT] it could be much worse. So please don't tell newbies to
              > search for r'^something'.[/color]

              How else would you match the beginning of a line in a multi-line string?

              Comment

              • Duncan Booth

                #8
                Re: Regular expression to match a #

                John Machin wrote:
                [color=blue][color=green]
                >> Alternatively for C style #includes search for r'^\s*#\s*inclu de\b'.[/color]
                >
                > Search for r'^something' can never be better/faster than match for
                > r'something', and with a dopey implementation of search [which
                > Python's re is NOT] it could be much worse. So please don't tell
                > newbies to search for r'^something'.
                >[/color]
                Search for r'^something' is always better than searching for r'something'
                when the spec requires the search to match only at the start of a line (on
                the principle that code that works is better than code which doesn't).

                It appears that this may be something the original poster wanted, so I
                stand by my suggestion.

                Comment

                • Aahz

                  #9
                  Re: Regular expression to match a #

                  In article <42fb45d7$1@new s.eftel.com>,
                  John Machin <sjmachin@lexic on.net> wrote:[color=blue]
                  >
                  >Search for r'^something' can never be better/faster than match for
                  >r'something' , and with a dopey implementation of search [which Python's
                  >re is NOT] it could be much worse. So please don't tell newbies to
                  >search for r'^something'.[/color]

                  You're somehow getting mixed up in thinking that "^" is some kind of
                  "not" operator -- it's the start of line anchor in this context.
                  --
                  Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

                  The way to build large Python applications is to componentize and
                  loosely-couple the hell out of everything.

                  Comment

                  • John Machin

                    #10
                    Re: Regular expression to match a #

                    Jeff Schwab wrote:[color=blue]
                    > John Machin wrote:
                    >[color=green]
                    >> Search for r'^something' can never be better/faster than match for
                    >> r'something', and with a dopey implementation of search [which
                    >> Python's re is NOT] it could be much worse. So please don't tell
                    >> newbies to search for r'^something'.[/color]
                    >
                    >
                    > How else would you match the beginning of a line in a multi-line string?[/color]

                    I beg your pardon -- I should have qualified that:

                    """
                    So please don't tell newbies to search for r'^something' when match of
                    r'something' does the job.
                    """

                    Comment

                    • John Machin

                      #11
                      Re: Regular expression to match a #

                      Duncan Booth wrote:[color=blue]
                      > John Machin wrote:
                      >
                      >[color=green][color=darkred]
                      >>>Alternativel y for C style #includes search for r'^\s*#\s*inclu de\b'.[/color]
                      >>
                      >>Search for r'^something' can never be better/faster than match for
                      >>r'something ', and with a dopey implementation of search [which
                      >>Python's re is NOT] it could be much worse. So please don't tell
                      >>newbies to search for r'^something'.
                      >>[/color]
                      >
                      > Search for r'^something' is always better than searching for r'something'
                      > when the spec requires the search to match only at the start of a line (on
                      > the principle that code that works is better than code which doesn't).
                      >
                      > It appears that this may be something the original poster wanted, so I
                      > stand by my suggestion.[/color]


                      We could well be lost in a semantic fog where at least one of us is
                      using "match" to mean "the match() method" and at least one of us is
                      using match to mean soemthing like "the outcome of using a search()
                      method [or a match() method]".

                      So I'll stand by my suggestion, too.

                      Comment

                      • John Machin

                        #12
                        Re: Regular expression to match a #

                        Aahz wrote:[color=blue]
                        > In article <42fb45d7$1@new s.eftel.com>,
                        > John Machin <sjmachin@lexic on.net> wrote:
                        >[color=green]
                        >>Search for r'^something' can never be better/faster than match for
                        >>r'something ', and with a dopey implementation of search [which Python's
                        >>re is NOT] it could be much worse. So please don't tell newbies to
                        >>search for r'^something'.[/color]
                        >
                        >
                        > You're somehow getting mixed up in thinking that "^" is some kind of
                        > "not" operator -- it's the start of line anchor in this context.[/color]

                        I can't imagine where you got that idea from.

                        If I change "[which Python's re is NOT]" to "[Python's re's search() is
                        not dopey]", does that help you?

                        The point was made in a context where the OP appeared to be reading a
                        line at a time and parsing it, and re.compile(r'so mething').match ()
                        would do the job; re.compile(r'^s omething').sear ch() will do the job too
                        -- BECAUSE ^ means start of line anchor -- but somewhat redundantly, and
                        very inefficiently in the failing case with dopey implementations of
                        search() (which apply match() at offsets 0, 1, 2, .....).

                        Comment

                        • Devan L

                          #13
                          Re: Regular expression to match a #

                          John Machin wrote:[color=blue]
                          > Aahz wrote:[color=green]
                          > > In article <42fb45d7$1@new s.eftel.com>,
                          > > John Machin <sjmachin@lexic on.net> wrote:
                          > >[color=darkred]
                          > >>Search for r'^something' can never be better/faster than match for
                          > >>r'something ', and with a dopey implementation of search [which Python's
                          > >>re is NOT] it could be much worse. So please don't tell newbies to
                          > >>search for r'^something'.[/color]
                          > >
                          > >
                          > > You're somehow getting mixed up in thinking that "^" is some kind of
                          > > "not" operator -- it's the start of line anchor in this context.[/color]
                          >
                          > I can't imagine where you got that idea from.
                          >
                          > If I change "[which Python's re is NOT]" to "[Python's re's search() is
                          > not dopey]", does that help you?
                          >
                          > The point was made in a context where the OP appeared to be reading a
                          > line at a time and parsing it, and re.compile(r'so mething').match ()
                          > would do the job; re.compile(r'^s omething').sear ch() will do the job too
                          > -- BECAUSE ^ means start of line anchor -- but somewhat redundantly, and
                          > very inefficiently in the failing case with dopey implementations of
                          > search() (which apply match() at offsets 0, 1, 2, .....).[/color]

                          I don't see much difference.
                          Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]
                          on win32
                          Type "copyright" , "credits" or "license()" for more information.

                          *************** *************** *************** *************** ****
                          Personal firewall software may warn about the connection IDLE
                          makes to its subprocess using this computer's internal loopback
                          interface. This connection is not visible on any external
                          interface and no data is sent to or received from the Internet.
                          *************** *************** *************** *************** ****

                          IDLE 1.1.1[color=blue][color=green][color=darkred]
                          >>> import timeit
                          >>> t1 = timeit.Timer('r e.search("^\w", " will not work")','import re')
                          >>> t1.timeit()[/color][/color][/color]
                          34.938577109660 628[color=blue][color=green][color=darkred]
                          >>> t2 = timeit.Timer('r e.match("\w"," will not work")','import re')
                          >>> t2.timeit()[/color][/color][/color]
                          31.381461330979 164[color=blue][color=green][color=darkred]
                          >>> 3.0/1000000[/color][/color][/color]
                          3.0000000000000 001e-006[color=blue][color=green][color=darkred]
                          >>> t1.timeit()[/color][/color][/color]
                          35.282282524734 228[color=blue][color=green][color=darkred]
                          >>> t2.timeit()[/color][/color][/color]
                          31.403153752781 463

                          ~4 second difference after a million times through seems to be trivial.
                          Then again, I haven't tested it for larger patterns and strings.

                          Comment

                          • John Machin

                            #14
                            Re: Regular expression to match a #

                            Devan L wrote:[color=blue]
                            > John Machin wrote:
                            >[color=green]
                            >>Aahz wrote:
                            >>[color=darkred]
                            >>>In article <42fb45d7$1@new s.eftel.com>,
                            >>>John Machin <sjmachin@lexic on.net> wrote:
                            >>>
                            >>>
                            >>>>Search for r'^something' can never be better/faster than match for
                            >>>>r'something ', and with a dopey implementation of search [which Python's
                            >>>>re is NOT] it could be much worse. So please don't tell newbies to
                            >>>>search for r'^something'.
                            >>>
                            >>>
                            >>>You're somehow getting mixed up in thinking that "^" is some kind of
                            >>>"not" operator -- it's the start of line anchor in this context.[/color]
                            >>
                            >>I can't imagine where you got that idea from.
                            >>
                            >>If I change "[which Python's re is NOT]" to "[Python's re's search() is
                            >>not dopey]", does that help you?
                            >>
                            >>The point was made in a context where the OP appeared to be reading a
                            >>line at a time and parsing it, and re.compile(r'so mething').match ()
                            >>would do the job; re.compile(r'^s omething').sear ch() will do the job too
                            >>-- BECAUSE ^ means start of line anchor -- but somewhat redundantly, and
                            >>very inefficiently in the failing case with dopey implementations of
                            >>search() (which apply match() at offsets 0, 1, 2, .....).[/color]
                            >
                            >
                            > I don't see much difference.[/color]

                            and I didn't expect that you would -- like I wrote above: "Python's re's
                            search() is not dopey".

                            Comment

                            • Devan L

                              #15
                              Re: Regular expression to match a #


                              John Machin wrote:[color=blue]
                              > Devan L wrote:[color=green]
                              > > John Machin wrote:
                              > >[color=darkred]
                              > >>Aahz wrote:
                              > >>
                              > >>>In article <42fb45d7$1@new s.eftel.com>,
                              > >>>John Machin <sjmachin@lexic on.net> wrote:
                              > >>>
                              > >>>
                              > >>>>Search for r'^something' can never be better/faster than match for
                              > >>>>r'something ', and with a dopey implementation of search [which Python's
                              > >>>>re is NOT] it could be much worse. So please don't tell newbies to
                              > >>>>search for r'^something'.
                              > >>>
                              > >>>
                              > >>>You're somehow getting mixed up in thinking that "^" is some kind of
                              > >>>"not" operator -- it's the start of line anchor in this context.
                              > >>
                              > >>I can't imagine where you got that idea from.
                              > >>
                              > >>If I change "[which Python's re is NOT]" to "[Python's re's search() is
                              > >>not dopey]", does that help you?
                              > >>
                              > >>The point was made in a context where the OP appeared to be reading a
                              > >>line at a time and parsing it, and re.compile(r'so mething').match ()
                              > >>would do the job; re.compile(r'^s omething').sear ch() will do the job too
                              > >>-- BECAUSE ^ means start of line anchor -- but somewhat redundantly, and
                              > >>very inefficiently in the failing case with dopey implementations of
                              > >>search() (which apply match() at offsets 0, 1, 2, .....).[/color]
                              > >
                              > >
                              > > I don't see much difference.[/color]
                              >
                              > and I didn't expect that you would -- like I wrote above: "Python's re's
                              > search() is not dopey".[/color]

                              Your wording makes it hard to distinguish what exactly is "dopey".

                              Comment

                              Working...