looking for a regular expression

Discussion in 'Python' started by ¨ì©³¦b²Ö¤°»ò°Ú¡H, Aug 1, 2006.

  1. I want words between 2 nearby commas with a specific word in them.

    For example,in the text blow:

    "We the people of the United States, in order to form a more perfect union,
    establish justice, insure domestic tranquility,......"

    I want "justice" and the words between the 2 nearest commas, which is

    establish justice

    All I can come up with is

    r",(.*?justice.*?),"

    but the result is

    in order to form a more perfect union, establish justice

    Apreciate any help.
    --
    ¤Ò§LªÌ¤£²»¤§¾¹ª«©Î´c¤§¬G¦³¹DªÌ¤£³B§g¤l©~«h¶Q¥ª¥Î§L«h¶Q¥k§LªÌ¤£²»¤§¾¹«D§g¤l
    ¤§¾¹¤£±o¤w¦Ó¥Î¤§«ï²H¬°¤W³Ó¦Ó¤£¬ü¦Ó¬ü¤§ªÌ¬O¼Ö±þ¤H¤Ò¼Ö±þ¤HªÌ«h¤£¥i±o§Ó©ó¤Ñ¤U
    ¨o¦N¨Æ©|¥ª¤¿¨Æ©|¥k°¾±N­x©~¥ª¤W±N­x©~¥k¨¥¥H³à§³B¤§±þ¤H¤§²³¥H«s´dª_¤§¾Ô³Ó¥H
    ³à§³B¤§¹D±`µL¦W¾ëÁö¤p¤Ñ¤U²ö¯à¦Ú«J¤ý­Y¯à¦u¤§¸Uª«±N¦Û»«¤Ñ¦a¬Û¦X¥H­°¥ÌÅS¥Á²ö
    ¤§¥O¦Ó¦Û§¡©l¨î¦³¦W¦W¥ç¬J¦³¤Ò¥ç±Nª¾ 210-64-83-81.adsl.dynamic.seed.net.tw®ü
    §@ªÌ¦b 06/08/01 21:35:48 ±q 210-64-83-81.adsl.dynamic.seed.net.tw ­×§ï³o½g¤å³¹
    §@ªÌ¦b 06/08/01 21:36:28 ±q 210-64-83-81.adsl.dynamic.seed.net.tw ­×§ï³o½g¤å³¹
    §@ªÌ¦b 06/08/01 21:37:08 ±q 210-64-83-81.adsl.dynamic.seed.net.tw ­×§ï³o½g¤å³¹
    §@ªÌ¦b 06/08/01 21:38:04 ±q 210-64-83-81.adsl.dynamic.seed.net.tw ­×§ï³o½g¤å³¹
     
    ¨ì©³¦b²Ö¤°»ò°Ú¡H, Aug 1, 2006
    #1
    1. Advertising

  2. ¨ì©³¦b²Ö¤°»ò°Ú¡H

    Duncan Booth Guest

    ¨ì©³¦b²Ö¤°»ò°Ú¡H wrote:

    > I want "justice" and the words between the 2 nearest commas, which is
    >
    > establish justice
    >
    > All I can come up with is
    >
    > r",(.*?justice.*?),"
    >
    > but the result is
    >
    > in order to form a more perfect union, establish justice
    >
    > Apreciate any help.


    ",([^,]*justice[^,]*),"

    But this is simpler if you don't use a regular expression:

    >>> s = "We the people of the United States, in order to form a more

    perfect union, establish justice, insure domestic tranquility,......"
    >>> [phrase for phrase in s.split(',') if 'justice' in phrase ]

    [' establish justice']
     
    Duncan Booth, Aug 1, 2006
    #2
    1. Advertising

  3. How about
    my_string = "We the people of the United States, in order to form a
    more perfect union, establish justice, insure domestic
    tranquility,......"
    print (x for x in my_string.split(",") if "justice" in x).next()

    This isn't a regular expression, but it gives what you're looking for.

    THN
     
    Thomas Nelson, Aug 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,307
  2. Replies:
    4
    Views:
    737
  3. Replies:
    0
    Views:
    366
  4. ProvoWallis
    Replies:
    3
    Views:
    280
    Roger Miller
    May 24, 2006
  5. Replies:
    4
    Views:
    343
    John Machin
    Jul 29, 2006
Loading...

Share This Page