a string problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • micklee74@hotmail.com

    #1

    a string problem

    hi

    if i have a some lines like this
    a ) "here is first string"
    b ) "here is string2"
    c ) "here is string3"

    When i specify i only want to print the lines that contains "string" ie
    the first line and not the others. If i use re module, how to compile
    the expression to do this? I tried the re module and using simple
    search() and everytime it gives me all the 3 lines that have "string"
    in it, whereas i only need line 1.
    If re module is not needed, how can i use string manipulation to do
    this? thanks

  • John Salerno

    #2
    Re: a string problem

    micklee74@hotma il.com wrote:[color=blue]
    > hi
    >
    > if i have a some lines like this
    > a ) "here is first string"
    > b ) "here is string2"
    > c ) "here is string3"
    >
    > When i specify i only want to print the lines that contains "string" ie
    > the first line and not the others. If i use re module, how to compile
    > the expression to do this? I tried the re module and using simple
    > search() and everytime it gives me all the 3 lines that have "string"
    > in it, whereas i only need line 1.
    > If re module is not needed, how can i use string manipulation to do
    > this? thanks
    >[/color]

    As far as re goes, you can search for the pattern '\bstring\b', which
    will find just the word 'string' itself. Not sure if there's a better
    way to do it with REs.

    And I'm actually ashamed to admit that I know the RE way, but not the
    regular string manipulation way, if there is one! This seems like
    something easy enough to do without REs though.

    Comment

    • micklee74@hotmail.com

      #3
      Re: a string problem


      John Salerno wrote:
      [color=blue]
      > micklee74@hotma il.com wrote:[color=green]
      > > hi
      > >
      > > if i have a some lines like this
      > > a ) "here is first string"
      > > b ) "here is string2"
      > > c ) "here is string3"
      > >
      > > When i specify i only want to print the lines that contains "string" ie
      > > the first line and not the others. If i use re module, how to compile
      > > the expression to do this? I tried the re module and using simple
      > > search() and everytime it gives me all the 3 lines that have "string"
      > > in it, whereas i only need line 1.
      > > If re module is not needed, how can i use string manipulation to do
      > > this? thanks
      > >[/color]
      >
      > As far as re goes, you can search for the pattern '\bstring\b', which
      > will find just the word 'string' itself. Not sure if there's a better
      > way to do it with REs.
      >
      > And I'm actually ashamed to admit that I know the RE way, but not the
      > regular string manipulation way, if there is one! This seems like
      > something easy enough to do without REs though.[/color]

      thanks !

      Comment

      • MTD

        #4
        Re: a string problem

        > When i specify i only want to print the lines that contains "string" ie[color=blue]
        > the first line and not the others. If i use re module, how to compile
        > the expression to do this? I tried the re module and using simple
        > search() and everytime it gives me all the 3 lines that have "string"
        > in it, whereas i only need line 1.[/color]

        That's because all three lines DO include the substring "string"

        Comment

        • micklee74@hotmail.com

          #5
          Re: a string problem


          John Salerno wrote:
          [color=blue]
          > micklee74@hotma il.com wrote:[color=green]
          > > hi
          > >
          > > if i have a some lines like this
          > > a ) "here is first string"
          > > b ) "here is string2"
          > > c ) "here is string3"
          > >
          > > When i specify i only want to print the lines that contains "string" ie
          > > the first line and not the others. If i use re module, how to compile
          > > the expression to do this? I tried the re module and using simple
          > > search() and everytime it gives me all the 3 lines that have "string"
          > > in it, whereas i only need line 1.
          > > If re module is not needed, how can i use string manipulation to do
          > > this? thanks
          > >[/color]
          >
          > As far as re goes, you can search for the pattern '\bstring\b', which
          > will find just the word 'string' itself. Not sure if there's a better
          > way to do it with REs.
          >
          > And I'm actually ashamed to admit that I know the RE way, but not the
          > regular string manipulation way, if there is one! This seems like
          > something easy enough to do without REs though.[/color]

          if RE has the \b and it works, can we look into the source of re and
          see how its done for \b ?

          Comment

          • micklee74@hotmail.com

            #6
            Re: a string problem


            John Salerno wrote:
            [color=blue]
            > micklee74@hotma il.com wrote:[color=green]
            > > hi
            > >
            > > if i have a some lines like this
            > > a ) "here is first string"
            > > b ) "here is string2"
            > > c ) "here is string3"
            > >
            > > When i specify i only want to print the lines that contains "string" ie
            > > the first line and not the others. If i use re module, how to compile
            > > the expression to do this? I tried the re module and using simple
            > > search() and everytime it gives me all the 3 lines that have "string"
            > > in it, whereas i only need line 1.
            > > If re module is not needed, how can i use string manipulation to do
            > > this? thanks
            > >[/color]
            >
            > As far as re goes, you can search for the pattern '\bstring\b', which
            > will find just the word 'string' itself. Not sure if there's a better
            > way to do it with REs.
            >
            > And I'm actually ashamed to admit that I know the RE way, but not the
            > regular string manipulation way, if there is one! This seems like
            > something easy enough to do without REs though.[/color]

            just curious , if RE has the \b and it works, can we look into the
            source of re and see how its done for \b ?

            Comment

            • John Salerno

              #7
              Re: a string problem

              micklee74@hotma il.com wrote:
              [color=blue]
              > just curious , if RE has the \b and it works, can we look into the
              > source of re and see how its done for \b ?[/color]

              I had a look in the sre module (which re seems to import), but I
              couldn't find much. I'm not the best at analyzing source code, though. :)

              What is it you want to know about \b? It searches for the empty string
              before and after a word (word being an alphanumeric character that can
              include underscores).

              A little more specific info is in the docs:

              Matches the empty string, but only at the beginning or end of a word. A
              word is defined as a sequence of alphanumeric or underscore characters,
              so the end of a word is indicated by whitespace or a non-alphanumeric,
              non-underscore character. Note that \b is defined as the boundary
              between \w and \ W, so the precise set of characters deemed to be
              alphanumeric depends on the values of the UNICODE and LOCALE flags.
              Inside a character range, \b represents the backspace character, for
              compatibility with Python's string literals.

              Comment

              • micklee74@hotmail.com

                #8
                Re: a string problem


                John Salerno wrote:[color=blue]
                > micklee74@hotma il.com wrote:
                >[color=green]
                > > just curious , if RE has the \b and it works, can we look into the
                > > source of re and see how its done for \b ?[/color]
                >
                > I had a look in the sre module (which re seems to import), but I
                > couldn't find much. I'm not the best at analyzing source code, though. :)
                >
                > What is it you want to know about \b? It searches for the empty string
                > before and after a word (word being an alphanumeric character that can
                > include underscores).
                >
                > A little more specific info is in the docs:
                >
                > Matches the empty string, but only at the beginning or end of a word. A
                > word is defined as a sequence of alphanumeric or underscore characters,
                > so the end of a word is indicated by whitespace or a non-alphanumeric,
                > non-underscore character. Note that \b is defined as the boundary
                > between \w and \ W, so the precise set of characters deemed to be
                > alphanumeric depends on the values of the UNICODE and LOCALE flags.
                > Inside a character range, \b represents the backspace character, for
                > compatibility with Python's string literals.[/color]

                thanks..actuall y i had seen \b in the docs before, just that it slipped
                my mind when i was doing the coding. was even meddling with look aheads
                ...which is not the answer anyway.
                well, since re has the \b, was wondering why there is no implementation
                of it in strings. So the idea of looking at the source or re on how
                it's done came to my mine..i suppose we have to go down to viewing the
                C source then..:-)

                Comment

                • Rune Strand

                  #9
                  Re: a string problem


                  micklee74@hotma il.com wrote:[color=blue]
                  > hi
                  >
                  > if i have a some lines like this
                  > a ) "here is first string"
                  > b ) "here is string2"
                  > c ) "here is string3"
                  >
                  > When i specify i only want to print the lines that contains "string" ie
                  > the first line and not the others. If i use re module, how to compile
                  > the expression to do this? I tried the re module and using simple
                  > search() and everytime it gives me all the 3 lines that have "string"
                  > in it, whereas i only need line 1.
                  > If re module is not needed, how can i use string manipulation to do
                  > this? thanks[/color]

                  If this is a RL-situation,
                  if mystring.endswi th('string') will do

                  Comment

                  • Maric Michaud

                    #10
                    Re: a string problem

                    Le Mardi 13 Juin 2006 15:59, John Salerno a écrit :[color=blue]
                    > And I'm actually ashamed to admit that I know the RE way, but not the
                    > regular string manipulation way, if there is one![/color]

                    eheh,

                    In [39]: import string

                    In [40]: sub, s1, s2 = 'string', 're string2, ,string1', 're string2, ,string'

                    In [41]: sub in [ e.strip(string. punctuation) for e in s1.split() ]
                    Out[41]: False

                    In [42]: sub in [ e.strip(string. punctuation) for e in s2.split() ]
                    Out[42]: True
                    [color=blue]
                    > This seems like
                    > something easy enough to do without REs though.[/color]

                    Yes, but python way seems a little faster

                    python2.4 -mtimeit -s "import re" "re.match('\bst ring\b', 're
                    string2, ,string1') and True"
                    100000 loops, best of 3: 7.3 usec per loop
                    python2.4 -mtimeit -s "import string" "'string' in [
                    e.strip(string. punctuation) for e in 're string2, ,string1'.split () ]"
                    100000 loops, best of 3: 6.99 usec per loop


                    --
                    _____________

                    Maric Michaud
                    _____________

                    Aristote - www.aristote.info
                    3 place des tapis
                    69004 Lyon
                    Tel: +33 426 880 097

                    Comment

                    • Mitja Trampus

                      #11
                      Re: a string problem

                      John Salerno wrote:[color=blue]
                      > micklee74@hotma il.com wrote:[color=green]
                      >> hi
                      >>
                      >> if i have a some lines like this
                      >> a ) "here is first string"
                      >> b ) "here is string2"
                      >> c ) "here is string3"
                      >>
                      >> When i specify i only want to print the lines that contains "string" ie[/color]
                      > ...
                      > And I'm actually ashamed to admit that I know the RE way, but not the
                      > regular string manipulation way, if there is one! This seems like
                      > something easy enough to do without REs though.[/color]

                      I'd just split it on whitespace, just like with the RE:
                      if "string" in "here is first string".split() : ...

                      Comment

                      Working...