regular expressions.

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

    regular expressions.

    Hey All,

    I have been playing around with REs and could not get the following
    code to run.

    import re
    vowel = r'[aeiou]'
    re.findall(vowe l, r"vowel")

    anything wrong I have done?

    Regards,
    Atul.

  • Peter Otten

    #2
    Re: regular expressions.

    Atul. wrote:
    I have been playing around with REs and could not get the following
    code to run.
    >
    import re
    vowel = r'[aeiou]'
    re.findall(vowe l, r"vowel")
    >
    anything wrong I have done?
    Yes. You didn't paste the traceback into your message.
    >>import re
    >>vowel = r'[aeiou]'
    >>re.findall(vo wel, r"vowel")
    ['o', 'e']

    It works as expected here.

    Peter

    Comment

    • Atul.

      #3
      Re: regular expressions.

      Yes. You didn't paste the traceback into your message.
      >
      >import re
      >vowel = r'[aeiou]'
      >re.findall(vow el, r"vowel")
      >
      ['o', 'e']
      >
      It works as expected here.
      >
      Peter
      When I key this input in IDLE it works but when I try to run the
      module it wont work.

      Comment

      • Peter Otten

        #4
        Re: regular expressions.

        Atul. wrote:
        >
        >Yes. You didn't paste the traceback into your message.
        >>
        >>import re
        >>vowel = r'[aeiou]'
        >>re.findall(vo wel, r"vowel")
        >>
        >['o', 'e']
        >>
        >It works as expected here.
        >>
        >Peter
        >
        When I key this input in IDLE it works but when I try to run the
        module it wont work.
        What's the name of your script? What happens when you run it? Does it print
        a traceback? If so, what does it say? Please cut and paste, don't
        paraphrase.

        Peter

        Comment

        • Atul.

          #5
          Re: regular expressions.

          On Aug 8, 4:22 pm, Peter Otten <__pete...@web. dewrote:
          Atul. wrote:
          >
          Yes. You didn't paste the traceback into your message.
          >
          >import re
          >vowel = r'[aeiou]'
          >re.findall(vow el, r"vowel")
          >
          ['o', 'e']
          >
          It works as expected here.
          >
          Peter
          >
          When I key this input in IDLE it works but when I try to run the
          module it wont work.
          >
          What's the name of your script? What happens when you run it? Does it print
          a traceback? If so, what does it say? Please cut and paste, don't
          paraphrase.
          >
          Peter
          This is something get when I run it like below. it does not print any
          output.

          atul@atul-desktop:~/Work/work/programs$ python fourth.py
          atul@atul-desktop:~/Work/work/programs$

          Comment

          • Atul.

            #6
            Re: regular expressions.

            On Aug 8, 4:33 pm, "Atul." <atulskulka...@ gmail.comwrote:
            On Aug 8, 4:22 pm, Peter Otten <__pete...@web. dewrote:
            >
            >
            >
            Atul. wrote:
            >
            >Yes. You didn't paste the traceback into your message.
            >
            >>import re
            >>vowel = r'[aeiou]'
            >>re.findall(vo wel, r"vowel")
            >
            >['o', 'e']
            >
            >It works as expected here.
            >
            >Peter
            >
            When I key this input in IDLE it works but when I try to run the
            module it wont work.
            >
            What's the name of your script? What happens when you run it? Does it print
            a traceback? If so, what does it say? Please cut and paste, don't
            paraphrase.
            >
            Peter
            >
            This is something get when I run it like below. it does not print any
            output.
            >
            atul@atul-desktop:~/Work/work/programs$ python fourth.py
            atul@atul-desktop:~/Work/work/programs$
            ok I get it thats coz, I dont print it. right? when I print it does
            she it.

            Comment

            • Diez B. Roggisch

              #7
              Re: regular expressions.

              Atul. schrieb:
              On Aug 8, 4:33 pm, "Atul." <atulskulka...@ gmail.comwrote:
              >On Aug 8, 4:22 pm, Peter Otten <__pete...@web. dewrote:
              >>
              >>
              >>
              >>Atul. wrote:
              >>>>Yes. You didn't paste the traceback into your message.
              >>>>>>>import re
              >>>>>>>vowel = r'[aeiou]'
              >>>>>>>re.finda ll(vowel, r"vowel")
              >>>>['o', 'e']
              >>>>It works as expected here.
              >>>>Peter
              >>>When I key this input in IDLE it works but when I try to run the
              >>>module it wont work.
              >>What's the name of your script? What happens when you run it? Does it print
              >>a traceback? If so, what does it say? Please cut and paste, don't
              >>paraphrase.
              >>Peter
              >This is something get when I run it like below. it does not print any
              >output.
              >>
              >atul@atul-desktop:~/Work/work/programs$ python fourth.py
              >atul@atul-desktop:~/Work/work/programs$
              >
              ok I get it thats coz, I dont print it. right? when I print it does
              she it.
              Yes.

              Diez

              Comment

              • Peter Otten

                #8
                Re: regular expressions.

                Atul. wrote:
                On Aug 8, 4:33 pm, "Atul." <atulskulka...@ gmail.comwrote:
                >On Aug 8, 4:22 pm, Peter Otten <__pete...@web. dewrote:
                >>
                >>
                >>
                Atul. wrote:
                >>
                >Yes. You didn't paste the traceback into your message.
                >>
                >>import re
                >>vowel = r'[aeiou]'
                >>re.findall(vo wel, r"vowel")
                >>
                >['o', 'e']
                >>
                >It works as expected here.
                >>
                >Peter
                >>
                When I key this input in IDLE it works but when I try to run the
                module it wont work.
                >>
                What's the name of your script? What happens when you run it? Does it
                print a traceback? If so, what does it say? Please cut and paste, don't
                paraphrase.
                >>
                Peter
                >>
                >This is something get when I run it like below. it does not print any
                >output.
                >>
                >atul@atul-desktop:~/Work/work/programs$ python fourth.py
                >atul@atul-desktop:~/Work/work/programs$
                >
                ok I get it thats coz, I dont print it. right? when I print it does
                she it.
                Heureka!

                Comment

                • Atul.

                  #9
                  Re: regular expressions.

                  The same file when I use with the following does not work.

                  import re
                  vowel =
                  r'[u"\u093e"u"\u09 3f"u"\u0940"u"\ u0941"u"\u0942" u"\u0943"u"\u09 44"u"\u0945"u"\ u0946"u"\u0947" u"\u0948"u"\u09 49"u"\u094a"u"\ u094b"u"\u094c"]'
                  print re.findall(vowe l, u"\u092f\u093e\ u0902\u091a\u09 4d\u092f\u093e" ,
                  re.UNICODE)



                  atul@atul-desktop:~/Work/work/programs$ python fourth.py
                  []
                  atul@atul-desktop:~/Work/work/programs$


                  is this the way to use Unicode in REs?

                  Regards,
                  Atul.

                  Comment

                  • Peter Otten

                    #10
                    Re: regular expressions.

                    Atul. wrote:
                    The same file when I use with the following does not work.
                    >
                    import re
                    vowel =
                    r'[u"\u093e"u"\u09 3f"u"\u0940"u"\ u0941"u"\u0942" u"\u0943"u"\u09 44"u"\u0945"u"\ u0946"u"\u0947" u"\u0948"u"\u09 49"u"\u094a"u"\ u094b"u"\u094c"]'
                    print re.findall(vowe l, u"\u092f\u093e\ u0902\u091a\u09 4d\u092f\u093e" ,
                    re.UNICODE)
                    >
                    >
                    >
                    atul@atul-desktop:~/Work/work/programs$ python fourth.py
                    []
                    atul@atul-desktop:~/Work/work/programs$
                    >
                    >
                    is this the way to use Unicode in REs?
                    No, u"..." is part of the string, not the character. The regex becomes

                    # untested
                    vowel = u'[\u093e\u093f\u0 940\u0941\u0942 \u0943\u0944\u0 945\u0946\u0947 \u0948\u0949\u0 94a\u094b\u094c]'

                    Peter

                    Comment

                    • Hrvoje Niksic

                      #11
                      Re: regular expressions.

                      "Atul." <atulskulkarni@ gmail.comwrites :
                      the following does not work.
                      >
                      import re
                      vowel =
                      r'[u"\u093e"u"\u09 3f"u"\u0940"u"\ u0941"u"\u0942" u"\u0943"u"\u09 44"u"\u0945"u"\ u0946"u"\u0947" u"\u0948"u"\u09 49"u"\u094a"u"\ u094b"u"\u094c"]'
                      Unfortunately you cannot embed arbitrary Python string constants
                      (u"...") in regular expressions. What does work is something like:
                      >>vowel = u'[\u093e\u093f\u0 940\u0941\u0942 \u0943\u0944\u0 945\u0946\u0947 \u0948\u0949\u0 94a\u094b\u094c]'
                      >>re.findall(vo wel, u"\u092f\u093e\ u0902\u091a\u09 4d\u092f\u093e" )
                      [u'\u093e', u'\u093e']

                      Comment

                      Working...