How To Check For [a-z] In String?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hank Kingwood

    How To Check For [a-z] In String?

    This is probably an easy question, but I can't find a function (maybe my
    syntax is off...) to search for [a-z] in a string. If someone would
    help out, I'd appreciate it!

    Also, how would you recommend searching for the following in the same
    string:
    [a-z]
    [A-Z]
    [0-9]
    -

    My approach would be to perform four separte checks, but I'm thinking
    there might be some cool approach that would use a dictionary or array.
    Ideas?

    Thanks,
    Hank

  • Hank Kingwood

    #2
    Re: How To Check For [a-z] In String?

    Also, how would I check if [a-z] is not in a string?

    Thanks again,
    Hank

    Hank Kingwood wrote:[color=blue]
    > This is probably an easy question, but I can't find a function (maybe my
    > syntax is off...) to search for [a-z] in a string. If someone would
    > help out, I'd appreciate it!
    >
    > Also, how would you recommend searching for the following in the same
    > string:
    > [a-z]
    > [A-Z]
    > [0-9]
    > -
    >
    > My approach would be to perform four separte checks, but I'm thinking
    > there might be some cool approach that would use a dictionary or array.
    > Ideas?
    >
    > Thanks,
    > Hank
    >[/color]

    Comment

    • Jay Dorsey

      #3
      Re: How To Check For [a-z] In String?



      Hank Kingwood wrote:[color=blue]
      > This is probably an easy question, but I can't find a function (maybe my
      > syntax is off...) to search for [a-z] in a string. If someone would
      > help out, I'd appreciate it!
      >
      > Also, how would you recommend searching for the following in the same
      > string:
      > [a-z]
      > [A-Z]
      > [0-9]
      > -
      >
      > My approach would be to perform four separte checks, but I'm thinking
      > there might be some cool approach that would use a dictionary or array.
      > Ideas?[/color]

      Are you looking for the actual string [a-z] in a string, or are you
      looking for the regular expression [a-z] (any one lowercase letter)

      Actual string:
      [color=blue][color=green][color=darkred]
      >>> import re
      >>> regex = re.compile('\[a-z\]')
      >>> regex.search('1 23123[a-z]adsfasfd').grou p()[/color][/color][/color]
      '[a-z]'

      Regex pattern:[color=blue][color=green][color=darkred]
      >>> import re
      >>> regex = re.compile('[a-z]')
      >>> regex.search('1 23123123a123123 b123123c').grou p()[/color][/color][/color]
      'a'[color=blue][color=green][color=darkred]
      >>> regex.findall(' 123123123a12312 3b123123c')[/color][/color][/color]
      ['a', 'b', 'c']

      You could also do:[color=blue][color=green][color=darkred]
      >>> import re
      >>> regex = re.compile('[a-z]|[A-Z]')
      >>> regex.findall(' 123a23423b13123 c123123A123B123 C')[/color][/color][/color]
      ['a', 'b', 'c', 'A', 'B', 'C']

      There are probably other ways to do it with list comprehensions, but
      does that help?



      Jay



      Comment

      • Jay Dorsey

        #4
        Re: How To Check For [a-z] In String?

        Hank Kingwood wrote:[color=blue]
        > Also, how would I check if [a-z] is not in a string?[/color]

        following my re examples:
        [color=blue][color=green][color=darkred]
        >>> import re
        >>> regex = re.compile('[^a-z]')
        >>> if regex.search('1 23123123132'): print "no lower letters!"[/color][/color][/color]
        ....
        no lower letters!

        There is a good regular expression tutorial on


        jay


        Comment

        • Jay Dorsey

          #5
          Re: How To Check For [a-z] In String?

          Jay Dorsey wrote:
          [color=blue][color=green][color=darkred]
          > >>> regex = re.compile('[a-z]|[A-Z]')
          > >>> regex.findall(' 123a23423b13123 c123123A123B123 C')[/color][/color]
          > ['a', 'b', 'c', 'A', 'B', 'C']
          >[/color]

          Line 1 above should have been [a-zA-Z] (although as posted it works as
          well). Its just prettier the second way :-)

          Jay


          Comment

          • Bob Gailer

            #6
            Re: How To Check For [a-z] In String?

            At 08:48 AM 10/2/2003, Hank Kingwood wrote:
            [color=blue]
            >This is probably an easy question, but I can't find a function (maybe my
            >syntax is off...) to search for [a-z] in a string. If someone would help
            >out, I'd appreciate it!
            >
            >Also, how would you recommend searching for the following in the same string:
            > [a-z]
            > [A-Z]
            > [0-9]
            > -
            >
            >My approach would be to perform four separte checks, but I'm thinking
            >there might be some cool approach that would use a dictionary or array. Ideas?[/color]

            Sounds like a perfect application for regular expressions. Check out the re
            module. Example:
            [color=blue][color=green][color=darkred]
            >>> import re
            >>> re.findall(r'[a-zA-Z0-9]*', 'This is fun')[/color][/color][/color]
            ['This', '', 'is', '', 'fun', '']

            Bob Gailer
            bgailer@alum.rp i.edu
            303 442 2625


            ---
            Outgoing mail is certified Virus Free.
            Checked by AVG anti-virus system (http://www.grisoft.com).
            Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003

            Comment

            • Christopher Koppler

              #7
              Re: How To Check For [a-z] In String?

              On Thu, 02 Oct 2003 14:56:10 GMT, Hank Kingwood
              <hank@bogusaddr ess.xyz> wrote:
              [color=blue]
              >Also, how would I check if [a-z] is not in a string?[/color]

              import string

              sometext = "whatever"
              if not string.lowercas e in sometext:
              <do what you wanted to do>
              [color=blue]
              >
              >Thanks again,
              >Hank
              >
              >Hank Kingwood wrote:[color=green]
              >> This is probably an easy question, but I can't find a function (maybe my
              >> syntax is off...) to search for [a-z] in a string. If someone would
              >> help out, I'd appreciate it!
              >>
              >> Also, how would you recommend searching for the following in the same
              >> string:
              >> [a-z][/color][/color]

              if string.lowercas e in sometext:
              [color=blue][color=green]
              >> [A-Z][/color][/color]

              if string.uppercas e in sometext: ...
              [color=blue][color=green]
              >> [0-9][/color][/color]

              if string.digits in sometext: ...
              [color=blue][color=green]
              >> -
              >>
              >> My approach would be to perform four separte checks, but I'm thinking
              >> there might be some cool approach that would use a dictionary or array.
              >> Ideas?
              >>
              >> Thanks,
              >> Hank
              >>[/color][/color]

              As others have shown, you can of course do this with regular
              expressions, but the string module is your friend in this case, and
              makes for very readable code, which is also unicode-ready, since
              string.(lower|u pper)case also contain accented and other characters.
              Just do a dir(string) to see what other goodies there are...

              --
              Christopher

              Comment

              • Christopher Koppler

                #8
                Re: How To Check For [a-z] In String?

                On Thu, 02 Oct 2003 16:13:59 GMT, myself <klapotec@chell o.at> wrote:

                [color=blue]
                > which is also unicode-ready, since
                >string.(lower| upper)case also contain accented and other characters.[/color]

                Ouch, I take it back (the unicode thing), it's just all the
                lower/uppercase letters from the latin-1 set, but not in unicode...

                Also, if you want to check for ONLY [a-z], you can check for
                string.lowercas e[0:26]...



                hasty, hasty, hasty...


                --
                Christopher

                Comment

                • Peter Otten

                  #9
                  Re: How To Check For [a-z] In String?

                  Christopher Koppler wrote:
                  [color=blue][color=green]
                  >>Also, how would I check if [a-z] is not in a string?[/color]
                  >
                  > import string
                  >
                  > sometext = "whatever"
                  > if not string.lowercas e in sometext:
                  > <do what you wanted to do>[/color]

                  Let's see:
                  [color=blue][color=green][color=darkred]
                  >>> import string
                  >>> sometext = "whatever"
                  >>> if not string.lowercas e in sometext:[/color][/color][/color]
                  .... print "do what you wanted to"
                  ....
                  do what you wanted to

                  I doubt that this is what you expected.

                  s1 in s2

                  tests if s1 is a substring of s2, but you want

                  def contains(s, chars):
                  for c in s:
                  if c in chars:
                  return True
                  return False
                  [color=blue]
                  > expressions, but the string module is your friend in this case, and
                  > makes for very readable code, which is also unicode-ready, since
                  > string.(lower|u pper)case also contain accented and other characters.
                  > Just do a dir(string) to see what other goodies there are...[/color]

                  Most of these are already available as methods of the str class and are
                  duplicated here only for backwards compatibility.

                  What's actually in string.lowercas e/uppercase depends on the locale, you
                  should by no means take latin-1 for granted.

                  You have already withdrawn the unicode-ready claim.

                  Nasty, nasty, nasty :-)

                  Peter

                  Comment

                  • Christopher Koppler

                    #10
                    Re: How To Check For [a-z] In String?

                    On Thu, 02 Oct 2003 19:01:27 +0200, Peter Otten <__peter__@web. de>
                    wrote:
                    [color=blue]
                    >Christopher Koppler wrote:
                    >[color=green][color=darkred]
                    >>>Also, how would I check if [a-z] is not in a string?[/color]
                    >>
                    >> import string
                    >>
                    >> sometext = "whatever"
                    >> if not string.lowercas e in sometext:
                    >> <do what you wanted to do>[/color]
                    >
                    >Let's see:
                    >[color=green][color=darkred]
                    >>>> import string
                    >>>> sometext = "whatever"
                    >>>> if not string.lowercas e in sometext:[/color][/color]
                    >... print "do what you wanted to"
                    >...
                    >do what you wanted to
                    >
                    >I doubt that this is what you expected.
                    >
                    >s1 in s2
                    >
                    >tests if s1 is a substring of s2, but you want
                    >
                    >def contains(s, chars):
                    > for c in s:
                    > if c in chars:
                    > return True
                    > return False
                    >[color=green]
                    >> expressions, but the string module is your friend in this case, and
                    >> makes for very readable code, which is also unicode-ready, since
                    >> string.(lower|u pper)case also contain accented and other characters.
                    >> Just do a dir(string) to see what other goodies there are...[/color]
                    >
                    >Most of these are already available as methods of the str class and are
                    >duplicated here only for backwards compatibility.
                    >
                    >What's actually in string.lowercas e/uppercase depends on the locale, you
                    >should by no means take latin-1 for granted.
                    >
                    >You have already withdrawn the unicode-ready claim.
                    >
                    >Nasty, nasty, nasty :-)
                    >
                    >Peter[/color]

                    <High embarassement mode>
                    Big oops. Yeah, that goes to show what happens if you multitask
                    yourself and don't let your computer do it - I'm preparing for the
                    second of the two Oracle Certified Associate exams, which I take
                    tomorrow, and thought I'd take some time out and maybe answer some
                    questions here... Seems I'm a bit too confused and disorganized to do
                    that right now...
                    </HEM>

                    --
                    Christopher

                    Comment

                    • Peter Abel

                      #11
                      Re: How To Check For [a-z] In String?

                      Hank Kingwood <hank@bogusaddr ess.xyz> wrote in message news:<e0Xeb.2$g Q.133058630@new ssvr12.news.pro digy.com>...[color=blue]
                      > Also, how would I check if [a-z] is not in a string?
                      >
                      > Thanks again,
                      > Hank
                      >
                      > Hank Kingwood wrote:[color=green]
                      > > This is probably an easy question, but I can't find a function (maybe my
                      > > syntax is off...) to search for [a-z] in a string. If someone would
                      > > help out, I'd appreciate it!
                      > >
                      > > Also, how would you recommend searching for the following in the same
                      > > string:
                      > > [a-z]
                      > > [A-Z]
                      > > [0-9]
                      > > -
                      > >
                      > > My approach would be to perform four separte checks, but I'm thinking
                      > > there might be some cool approach that would use a dictionary or array.
                      > > Ideas?
                      > >
                      > > Thanks,
                      > > Hank
                      > >[/color][/color]

                      Use regular expressions.
                      [color=blue][color=green][color=darkred]
                      >>> import re
                      >>> any_string="fin d a-z, A-Z and 0-9, **+#-#?** no specials"
                      >>> re_search=re.co mpile(r'(\w+)')
                      >>> re_search.finda ll(any_string)[/color][/color][/color]
                      ['find', 'a', 'z', 'A', 'Z', 'and', '0', '9', 'no', 'specials'][color=blue][color=green][color=darkred]
                      >>> # Or
                      >>> re_search=re.co mpile(r'(\w)')
                      >>> re_search.finda ll(any_string)[/color][/color][/color]
                      ['f', 'i', 'n', 'd', 'a', 'z', 'A', 'Z', 'a', 'n', 'd', '0', '9', 'n',
                      'o', 's', 'p', 'e', 'c', 'i', 'a', 'l', 's'][color=blue][color=green][color=darkred]
                      >>>[/color][/color][/color]

                      The **\w** means a-z plus A-Z plus 0-9 plus _.
                      If you doen't want to get the underline you have to define:
                      [a-zA-Z0-9] instead.

                      For more information read the docu for the re-module.

                      Regards
                      Peter

                      Comment

                      Working...