Thread: get string

    #1
  1. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    43
    Rep Power
    0

    get string


    i have string which always contain [#]

    i.e.
    werg gg eg eg[34] asdff
    wef awef [2] dfg
    [34]dfg rgf

    i am processing string by string, what is the best way to extract the numbers in between the [ and ]s

    chssre
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    If that's the only number in the line:

    Code:
    left_bracket_pos = s.index('[')
    right_bracket_pos = s.index(']')
    
    num = int(s[left_bracket_pos+1:right_bracket_pos])
    Alternately:

    Code:
    import re
    match = re.search("\d+", s)
    print match.group(0)

    Comments on this post

    • Yegg` agrees
    Last edited by sfb; September 15th, 2005 at 09:55 AM.

IMN logo majestic logo threadwatch logo seochat tools logo