#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 1999
    Location
    Singapore
    Posts
    8
    Rep Power
    0

    Regular expression for this?


    What is the regular expression to get a format of (\S+ \S+) which add together no more then 25 characters?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    If I understand correctly, you want to match on two 'words' of non-whitespace characters, separated by a single space, whose total length is <= 25 characters (including the space).

    This is doable in a single regular expression, but not easily. You could do something like:

    regex= r'(\S \S{1,23})|(\S{2} \S{1,22})|(\S{3} \S{1,21})|...|(\S{23} \S)'

    Where '...' in the above needs to be filled with all the intermediate combinations.

    but this would be very cumbersome and probably highly inefficient.

    I think it would be better to match on r'\S+ \S+' and then filter out the matches that are too long.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    Originally Posted by DevCoach
    I think it would be better to match on r'\S+ \S+' and then filter out the matches that are too long.
    This is the correct way of doing it.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    Originally Posted by DevCoach
    I think it would be better to match on r'\S+ \S+' and then filter out the matches that are too long.
    That's the sanest thing I've heard about regexes in a long while.

IMN logo majestic logo threadwatch logo seochat tools logo