trying to match a string

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

    trying to match a string

    Hi,

    Hi,

    I am taking a string as an input from the user and it should only
    contain the chars:L , M or R

    I tried the folllowing in kodos but they are still not perfect:

    [^A-K,^N-Q,^S-Z,^0-9]
    [L][M][R]
    [LRM]?L?[LRM]? etc but they do not exactly meet what I need.

    For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.

    regards,
    SZ

    The string may or may not have all the three chars.
  • John Machin

    #2
    Re: trying to match a string

    On Jul 18, 8:33 pm, arnimavidyar... @gmail.com wrote:
    Hi,
    >
    Hi,
    >
    I am taking a string as an input from the user and it should only
    contain the chars:L , M or R
    >
    I tried the folllowing in kodos but they are still not perfect:
    >
    [^A-K,^N-Q,^S-Z,^0-9]
    [L][M][R]
    [LRM]?L?[LRM]? etc but they do not exactly meet what I need.
    >
    For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.
    >
    regards,
    SZ
    >
    The string may or may not have all the three chars.
    re.match(r'[LMR]+\Z', your_string)

    in English: one or more of L, M , or R, followed by the end of the
    string.

    Comment

    • oj

      #3
      Re: trying to match a string

      On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
      Hi,
      >
      Hi,
      >
      I am taking a string as an input from the user and it should only
      contain the chars:L , M or R
      >
      I tried the folllowing in kodos but they are still not perfect:
      >
      [^A-K,^N-Q,^S-Z,^0-9]
      [L][M][R]
      [LRM]?L?[LRM]? etc but they do not exactly meet what I need.
      >
      For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.
      >
      regards,
      SZ
      >
      The string may or may not have all the three chars.
      With regular expressions, [^LRM] matches a character that isn't L, R
      or M. So:

      import re

      var = "LRLRLRLNR"

      if re.search(r'[^LRM]', var):
      print "Invalid"

      Comment

      • John Machin

        #4
        Re: trying to match a string

        On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
        On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
        >
        >
        >
        Hi,
        >
        Hi,
        >
        I am taking a string as an input from the user and it should only
        contain the chars:L , M or R
        >
        I tried the folllowing in kodos but they are still not perfect:
        >
        [^A-K,^N-Q,^S-Z,^0-9]
        [L][M][R]
        [LRM]?L?[LRM]? etc but they do not exactly meet what I need.
        >
        For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.
        >
        regards,
        SZ
        >
        The string may or may not have all the three chars.
        >
        With regular expressions, [^LRM] matches a character that isn't L, R
        or M. So:
        >
        import re
        >
        var = "LRLRLRLNR"
        >
        if re.search(r'[^LRM]', var):
        print "Invalid"
        Fails if var refers to the empty string.

        Comment

        • oj

          #5
          Re: trying to match a string

          On Jul 18, 12:10 pm, John Machin <sjmac...@lexic on.netwrote:
          On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
          >
          >
          >
          On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
          >
          Hi,
          >
          Hi,
          >
          I am taking a string as an input from the user and it should only
          contain the chars:L , M or R
          >
          I tried the folllowing in kodos but they are still not perfect:
          >
          [^A-K,^N-Q,^S-Z,^0-9]
          [L][M][R]
          [LRM]?L?[LRM]? etc but they do not exactly meet what I need.
          >
          For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that..
          >
          regards,
          SZ
          >
          The string may or may not have all the three chars.
          >
          With regular expressions, [^LRM] matches a character that isn't L, R
          or M. So:
          >
          import re
          >
          var = "LRLRLRLNR"
          >
          if re.search(r'[^LRM]', var):
              print "Invalid"
          >
          Fails if var refers to the empty string.
          No it doesn't, it succeeds if var is an empty string. An empty string
          doesn't contain characters that are not L, R or M.

          The OP doesn't specify whether an empty string is valid or not. My
          interpretation was that an empty string would be valid.

          Comment

          • Andrew Freeman

            #6
            Re: trying to match a string

            oj wrote:
            On Jul 18, 12:10 pm, John Machin <sjmac...@lexic on.netwrote:
            >
            >On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
            >>
            >>
            >>
            >>
            >>On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
            >>>
            >>>Hi,
            >>>>
            >>>Hi,
            >>>>
            >>>I am taking a string as an input from the user and it should only
            >>>contain the chars:L , M or R
            >>>>
            >>>I tried the folllowing in kodos but they are still not perfect:
            >>>>
            >>>[^A-K,^N-Q,^S-Z,^0-9]
            >>>[L][M][R]
            >>>[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
            >>>>
            >>>For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.
            >>>>
            >>>regards,
            >>>SZ
            >>>>
            >>>The string may or may not have all the three chars.
            >>>>
            >>With regular expressions, [^LRM] matches a character that isn't L, R
            >>or M. So:
            >>>
            >>import re
            >>>
            >>var = "LRLRLRLNR"
            >>>
            >>if re.search(r'[^LRM]', var):
            >> print "Invalid"
            >>>
            >Fails if var refers to the empty string.
            >>
            >
            No it doesn't, it succeeds if var is an empty string. An empty string
            doesn't contain characters that are not L, R or M.
            >
            The OP doesn't specify whether an empty string is valid or not. My
            interpretation was that an empty string would be valid.
            >
            Why not just use * instead of + like:

            if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of string; $ means end of string
            print "Invalid"

            This will *only* print invalid when there is a character other than L, R, or M or a empty string.



            Comment

            • Andrew Freeman

              #7
              Re: trying to match a string

              Andrew Freeman wrote:
              oj wrote:
              >On Jul 18, 12:10 pm, John Machin <sjmac...@lexic on.netwrote:
              >>
              >>On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
              >>>
              >>>
              >>>
              >>>
              >>>On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
              >>>>
              >>>>Hi,
              >>>> Hi,
              >>>> I am taking a string as an input from the user and it
              >>>>should only
              >>>>contain the chars:L , M or R
              >>>> I tried the folllowing in kodos but they are still not
              >>>>perfect:
              >>>> [^A-K,^N-Q,^S-Z,^0-9]
              >>>>[L][M][R]
              >>>>[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
              >>>> For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N'
              >>>>.like that.
              >>>> regards,
              >>>>SZ
              >>>> The string may or may not have all the three chars.
              >>>>>
              >>>With regular expressions, [^LRM] matches a character that isn't L, R
              >>>or M. So:
              >>> import re
              >>> var = "LRLRLRLNR"
              >>> if re.search(r'[^LRM]', var):
              >>> print "Invalid"
              >>>>
              >>Fails if var refers to the empty string.
              >>>
              >>
              >No it doesn't, it succeeds if var is an empty string. An empty string
              >doesn't contain characters that are not L, R or M.
              >>
              >The OP doesn't specify whether an empty string is valid or not. My
              >interpretati on was that an empty string would be valid.
              >>
              Why not just use * instead of + like:
              >
              if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
              string; $ means end of string
              print "Invalid"
              >
              This will *only* print invalid when there is a character other than L,
              R, or M or a empty string.
              >
              Sorry, forget the beginning and ending markers, I just tried it out, it
              doesn't work.
              use this instead:


              if re.search(r'[^LRM]*', var):
              print "Invalid"

              Comment

              • arni

                #8
                Re: trying to match a string

                On Jul 18, 7:51 pm, Andrew Freeman <alif...@gmail. comwrote:
                Andrew Freeman wrote:
                oj wrote:
                On Jul 18, 12:10 pm, John Machin <sjmac...@lexic on.netwrote:
                >
                >On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
                >
                >>On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
                >
                >>>Hi,
                >>>        Hi,
                >>>        I am taking a string as an input from the user and it
                >>>should only
                >>>contain the chars:L , M or R
                >>>        I tried the folllowing in kodos but they are still not
                >>>perfect:
                >>>        [^A-K,^N-Q,^S-Z,^0-9]
                >>>[L][M][R]
                >>>[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
                >>>        For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N'
                >>>.like that.
                >>>        regards,
                >>>SZ
                >>>        The string may or may not have all the three chars.
                >
                >>With regular expressions, [^LRM] matches a character that isn't L, R
                >>or M. So:
                >>      import re
                >>      var = "LRLRLRLNR"
                >>      if re.search(r'[^LRM]', var):
                >>    print "Invalid"
                >
                >Fails if var refers to the empty string.
                >
                No it doesn't, it succeeds if var is an empty string. An empty string
                doesn't contain characters that are not L, R or M.
                >
                The OP doesn't specify whether an empty string is valid or not. My
                interpretation was that an empty string would be valid.
                >
                Why not just use * instead of + like:
                >
                if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
                string; $ means end of string
                   print "Invalid"
                >
                This will *only* print invalid when there is a character other than L,
                R, or M or a empty string.
                >
                Sorry, forget the beginning and ending markers, I just tried it out, it
                doesn't work.
                use this instead:
                >
                if re.search(r'[^LRM]*', var):
                   print "Invalid"
                I was using kodos to check the regex.I should have used the IDE
                instead.Thanks a lot again.

                Comment

                • John S

                  #9
                  Re: trying to match a string

                  On Jul 18, 7:51 am, Andrew Freeman <alif...@gmail. comwrote:
                  Andrew Freeman wrote:
                  oj wrote:
                  On Jul 18, 12:10 pm, John Machin <sjmac...@lexic on.netwrote:
                  >
                  >On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
                  >
                  >>On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
                  >
                  >>>Hi,
                  >>> Hi,
                  >>> I am taking a string as an input from the user and it
                  >>>should only
                  >>>contain the chars:L , M or R
                  >>> I tried the folllowing in kodos but they are still not
                  >>>perfect:
                  >>> [^A-K,^N-Q,^S-Z,^0-9]
                  >>>[L][M][R]
                  >>>[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
                  >>> For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N'
                  >>>.like that.
                  >>> regards,
                  >>>SZ
                  >>> The string may or may not have all the three chars.
                  >
                  >>With regular expressions, [^LRM] matches a character that isn't L, R
                  >>or M. So:
                  >> import re
                  >> var = "LRLRLRLNR"
                  >> if re.search(r'[^LRM]', var):
                  >> print "Invalid"
                  >
                  >Fails if var refers to the empty string.
                  >
                  No it doesn't, it succeeds if var is an empty string. An empty string
                  doesn't contain characters that are not L, R or M.
                  >
                  The OP doesn't specify whether an empty string is valid or not. My
                  interpretation was that an empty string would be valid.
                  >
                  Why not just use * instead of + like:
                  >
                  if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
                  string; $ means end of string
                  print "Invalid"
                  >
                  This will *only* print invalid when there is a character other than L,
                  R, or M or a empty string.
                  >
                  Sorry, forget the beginning and ending markers, I just tried it out, it
                  doesn't work.
                  use this instead:
                  >
                  if re.search(r'[^LRM]*', var):
                  print "Invalid"
                  This won't work -- every string in the universe contains 0 or more
                  characters which are not 'L', 'R', or 'M'. That is, the regular
                  expression X* could match the empty string, which can be found in all
                  strings.

                  Comment

                  • oj

                    #10
                    Re: trying to match a string

                    >
                    Why not just use * instead of + like:
                    >
                    if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
                    string; $ means end of string
                       print "Invalid"
                    >
                    This will *only* print invalid when there is a character other than L,
                    R, or M or a empty string.
                    >
                    Sorry, forget the beginning and ending markers, I just tried it out, it
                    doesn't work.
                    use this instead:
                    >
                    if re.search(r'[^LRM]*', var):
                       print "Invalid"
                    No, that's broken.

                    That searches for any number of invalid characters. Even 0, so it
                    ALWAYS matches, no matter what string you give it.

                    My regex worked in the first place, you're complicating it needlessly.
                    The presence of one invalid character makes the string invalid, so why
                    not just search for one? Similarly, there's no need to stick in the
                    beginning and end markers - you're not trying to match the entire
                    string, just find part of it that is invalid.

                    However, I think the sets solution by Scott David Daniels is the most
                    elegant method put forward.

                    Comment

                    • Andrew Freeman

                      #11
                      Re: trying to match a string

                      oj wrote:
                      >>Why not just use * instead of + like:
                      >>>
                      >>if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
                      >>string; $ means end of string
                      >> print "Invalid"
                      >>>
                      >>This will *only* print invalid when there is a character other than L,
                      >>R, or M or a empty string.
                      >>>
                      >Sorry, forget the beginning and ending markers, I just tried it out, it
                      >doesn't work.
                      >use this instead:
                      >>
                      >if re.search(r'[^LRM]*', var):
                      > print "Invalid"
                      >>
                      >
                      No, that's broken.
                      >
                      That searches for any number of invalid characters. Even 0, so it
                      ALWAYS matches, no matter what string you give it.
                      >
                      My regex worked in the first place, you're complicating it needlessly.
                      The presence of one invalid character makes the string invalid, so why
                      not just search for one? Similarly, there's no need to stick in the
                      beginning and end markers - you're not trying to match the entire
                      string, just find part of it that is invalid.
                      >
                      However, I think the sets solution by Scott David Daniels is the most
                      elegant method put forward.
                      --

                      >
                      I see your point after rereading the question, he only wants to match L
                      R and M
                      let me revise it please:

                      To show if valid:

                      if re.search(r'^[LRM]*$', 'LM'):
                      print 'Valid'

                      To show if invalid,

                      if re.search(r'^[^LRM]*$', '0'):
                      print 'Inalid'

                      Example session:
                      >>import re
                      >>def match(var):
                      >> if re.search(r'^[LRM]*$', var):
                      ........ print 'Valid'
                      ........ else:
                      ........ print 'Invalid'
                      >>>
                      >>eg = 'LRLRLRLRLM'
                      >>match(eg)
                      Valid
                      >>fg = 'LRLRLRNL'
                      >>match(fg)
                      Invalid
                      --
                      Andrew


                      Comment

                      • John Machin

                        #12
                        Re: trying to match a string

                        On Jul 19, 12:04 pm, Andrew Freeman <alif...@gmail. comwrote:
                        To show if valid:
                        >
                        if re.search(r'^[LRM]*$', 'LM'):
                        print 'Valid'
                        >
                        A couple of points:
                        (1) Instead of search(r'^blahb lah', ...) use match(r'blahbla h', ...)
                        (2) You need to choose your end-anchor correctly; your pattern is
                        permitting a newline at the end:
                        >>re.search(r '^[LRM]*$', 'LM\n')
                        <_sre.SRE_Mat ch object at 0x00B9E528>
                        >>>

                        Comment

                        • Andrew Freeman

                          #13
                          Re: trying to match a string

                          John Machin wrote:
                          On Jul 19, 12:04 pm, Andrew Freeman <alif...@gmail. comwrote:
                          >
                          >To show if valid:
                          >>
                          >if re.search(r'^[LRM]*$', 'LM'):
                          > print 'Valid'
                          >>
                          >>
                          >
                          A couple of points:
                          (1) Instead of search(r'^blahb lah', ...) use match(r'blahbla h', ...)
                          (2) You need to choose your end-anchor correctly; your pattern is
                          permitting a newline at the end:
                          >
                          >
                          >>>re.search(r' ^[LRM]*$', 'LM\n')
                          >>>>
                          <_sre.SRE_Mat ch object at 0x00B9E528>
                          >
                          --

                          >
                          Thanks for your pointers, however have a question regarding the first:
                          >>import re
                          >>def my_match(var): # my original
                          >> if re.search(r'^[LRM]*$', var):
                          >> print 'Valid'
                          >> else:
                          >> print 'invalid'
                          >>def other_match(var ): # your suggestion, I believe
                          >> if re.search(r'[LRM]*$', var):
                          >> print 'Valid'
                          >> else:
                          >> print 'Invalid'
                          >>>
                          >>eg = 'LRLRLRLRLM'
                          >>fg = 'LRLRLRNL'
                          >>my_match(eg )
                          Valid # correct!
                          >>my_match(fg )
                          Invaild # correct!
                          >>>
                          >>other_match(e g)
                          Valid # correct!
                          >>other_match(f g)
                          Vaild # INCORRECT, please explain

                          I believe other_match was your suggestion; to remove the ^
                          my_match is just a renamed version of my original function

                          Point 2:
                          Yes, I totally agree with you on point 2, let me try to combine my
                          knowledge and make a satisfactory function.
                          >>def final_match(var ):
                          >> if re.search(r'^[LRM]*\Z', var): # replace $ with \Z to limit
                          newlines
                          .... print 'Valid'
                          .... else:
                          .... print 'Invalid'
                          >> final_match(eg)
                          Valid
                          >>final_match(f g)
                          Invalid
                          >>final_match(e g + '\n')
                          Invalid

                          So, in conclusion, is this function satisfactory?

                          def match(var):
                          if re.search(r'^[LRM]*\Z', var):
                          print 'Valid'
                          else:
                          print 'Invalid'
                          --
                          Andrew

                          Comment

                          • Andrew Freeman

                            #14
                            Re: trying to match a string

                            Andrew Freeman wrote:
                            John Machin wrote:
                            >A couple of points:
                            >(1) Instead of search(r'^blahb lah', ...) use match(r'blahbla h', ...)
                            >(2) You need to choose your end-anchor correctly; your pattern is
                            >permitting a newline at the end:
                            I forgot to change search to match. This should be better:

                            def match(var):
                            if re.match(r'[LRM]*\Z', var):
                            return True
                            else:
                            return False

                            I was also thinking if you had a list of these items needing to be
                            verified you could use this:
                            >>l = ['LLMMRR', '00thLL', 'L', '\n']
                            >>out = []
                            >>map(lambda i: match(i)==False or out.append(i), l)
                            >>print out
                            ['LLMMRR', 'L']

                            --
                            Andrew

                            Comment

                            • John Machin

                              #15
                              Re: trying to match a string

                              On Jul 20, 5:00 am, Andrew Freeman <alif...@gmail. comwrote:
                              Andrew Freeman wrote:
                              John Machin wrote:
                              A couple of points:
                              (1) Instead of search(r'^blahb lah', ...) use match(r'blahbla h', ...)
                              (2) You need to choose your end-anchor correctly; your pattern is
                              permitting a newline at the end:
                              >
                              I forgot to change search to match. This should be better:
                              >
                              def match(var):
                              if re.match(r'[LRM]*\Z', var):
                              return True
                              else:
                              return False
                              A bit wordy ...

                              if blahblah:
                              return True
                              else:
                              return False

                              can in total generality be replaced by:

                              return blahblah

                              >
                              I was also thinking if you had a list of these items needing to be
                              verified you could use this:
                              You could, but I suggest you don't use it in a job interview :-)
                              >>l = ['LLMMRR', '00thLL', 'L', '\n']
                              (1) Don't use 'L'.lower() as a name; it slows down reading as people
                              need to fire up their mental parser to distinguish it from the result
                              of 3 - 2
                              >>out = []
                              >>map(lambda i: match(i)==False or out.append(i), l)
                              (2) Read PEP 8
                              (3) blahblah == False ==not blahblah
                              (4) You didn't show the output from map() i.e. something like [None,
                              True, None, True]
                              (5) or out.append(...) is a baroque use of a side-effect, and is quite
                              unnecessary. If you feel inexorably drawn to following the map way,
                              read up on the filter and reduce functions. Otherwise learn about list
                              comprehensions and generators.
                              >>print out
                              ['LLMMRR', 'L']
                              >
                              Consider this:
                              >>import re
                              >>alist = ['LLMMRR', '00thLL', 'L', '\n']
                              >>zeroplusLRM = re.compile(r'[LRM]*\Z').match
                              >>filter(zeropl usLRM, alist)
                              ['LLMMRR', 'L']
                              >>[x for x in alist if zeroplusLRM(x)]
                              ['LLMMRR', 'L']
                              >>>
                              Cheers,
                              John

                              Comment

                              Working...