re.findall(a patern,'function(dsf sdf sdf)')

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gcmartijn@gmail.com

    #1

    re.findall(a patern,'function(dsf sdf sdf)')

    H!

    First I have some random string below.

    bla = """ <script type="text/javascript">
    // <![CDATA[

    var bla = new Blaobject("argh 1a", "argh 2a", "24", 24, 345)

    function la( tec )
    {
    etc etc
    }

    function other thing( ){

    var two = new BlaObject("argh 1b", "argh 2b", ""+(sv), ""+(2f),
    "4");

    bla die bla
    }

    // ]]>
    </script> """


    Now I'm trying to get each BlaObject with the first (variable)
    function argument

    And I can say that this isn't working
    for a in re.findall(r'([BlaObject ])(.*)([)] *)',bla):
    print a

    The output must be something like:
    # ('BlaObject','a rgh 1a')
    # ('BlaObject','a rgh 1a')
    or
    # Blaobject("argh 1a", "argh 2a", "24", 24, 345)
    # BlaObject("argh 1b", "argh 2b", ""+(sv), ""+(2f), "4");


    My simple idea was to
    a. the start position is the BlaObject
    b. the stop position is the character ) (not ); because its a
    javascript function)
    c. the output [a (everything between) b]

    Who knows the answer ?

    Thanks very much,
    GCMartijn
  • Lie

    #2
    Re: re.findall(a patern,'functio n(dsf sdf sdf)')

    On Jul 26, 5:03 pm, gcmart...@gmail .com wrote:
    H!
    >
    First I have some random string below.
    >
    bla = """          <script type="text/javascript">
                    // <![CDATA[
    >
                    var bla = new Blaobject("argh 1a", "argh 2a", "24", 24, 345)
    >
                    function la( tec )
                    {
                      etc etc
                    }
    >
                    function other thing( ){
    >
                      var two = new BlaObject("argh 1b", "argh 2b", ""+(sv), ""+(2f), "4");
    >
                      bla die bla
                    }
    >
                    // ]]>
              </script      """
    >
    Now I'm trying to get each BlaObject with the first (variable)
    function argument
    >
    First of all, since you're dealing with Javascript, which is case-
    sensitive, Blaobject and BlaObject means different thing, if the dummy
    code is real, it'd have raised a name not found error.
    And I can say that this isn't working
    for a in re.findall(r'([BlaObject ])(.*)([)] *)',bla):
        print a
    Of course that doesn't work, you've put BlaObject in a square bracket
    (character class notation), which means the re module would search for
    _a single letter_ that exist inside the square bracket. Then you do a
    '.*', a greedy match-all, something that you generally don't want to
    do. Next is the '[)] *', a character class containing only a single
    character is the same as the character itself, and the zero-or-more-
    repetition (*) is applied to the white space after the character
    class, not to the character class itself.

    In short, the regular expression you used doesn't seem to be an effort
    to solve the problem. In other words, you haven't read the regular
    expression docs: http://docs.python.org/lib/module-re.html . In other
    words, it's useless to talk with you until then.

    (snip)

    Comment

    • gcmartijn@gmail.com

      #3
      Re: re.findall(a patern,'functio n(dsf sdf sdf)')

      In short, the regular expression you used doesn't seem to be an effort
      to solve the problem. In other words, you haven't read the regular
      expression docs:http://docs.python.org/lib/module-re.html. In other
      words, it's useless to talk with you until then.
      >
      Its a combination
      - I don't understand english very good (yet)
      - For me its hard to learn the re , I will try to search again at
      google for examples and do some copy past things.

      Comment

      • Fredrik Lundh

        #4
        Re: re.findall(a patern,'functio n(dsf sdf sdf)')

        gcmartijn@gmail .com wrote:
        - For me its hard to learn the re , I will try to search again at
        google for examples and do some copy past things.
        this might be useful when figuring out how RE:s work:

        regular expressions, python regular expressions, debugging regular expressions, developing regular expressions, develop regular expressions, debug regular expressions, python regex, python re, python gui regex, python gui regular expressions


        also, don't forget the following guideline:

        "Some people, when confronted with a problem, think 'I know,
        I'll use regular expressions.' Now they have two problems."

        some advice:

        - Keep the RE:s simple. You can often simplify things a lot by doing
        multiple searches, or even by applying a second RE on the results from
        the first. In this case, you could use one RE to search for BlaObject,
        and then use another one to extract the first argument.

        - Ordinary string methods (e.g. find, partition, split) are often a very
        capable alternative (in combination with simple RE:s). In your case,
        for JavaScript code that's as regular as the one in your example, you
        can split the string on "BlaObject( " and then use partition to strip off
        the first argument.

        - only use RE:s to read specialized file formats if you know exactly
        what you're doing; there's often a ready-made library that does it much
        better.

        - The regular expression machinery is not a parser. You cannot handle
        all possible syntaxes with it, so don't even try.

        </F>

        Comment

        • gcmartijn@gmail.com

          #5
          Re: re.findall(a patern,'functio n(dsf sdf sdf)')

          On 26 jul, 14:25, Fredrik Lundh <fred...@python ware.comwrote:
          gcmart...@gmail .com wrote:
          - For me its hard to learn the re , I will try to search again at
          google for examples and do some copy past things.
          >
          this might be useful when figuring out how RE:s work:
          >
               http://kodos.sourceforge.net/
          >
          also, don't forget the following guideline:
          >
              "Some people, when confronted with a problem, think 'I know,
              I'll use regular expressions.'   Now they have two problems."
          >
          some advice:
          >
          - Keep the RE:s simple.  You can often simplify things a lot by doing
          multiple searches, or even by applying a second RE on the results from
          the first.  In this case, you could use one RE to search for BlaObject,
          and then use another one to extract the first argument.
          >
          - Ordinary string methods (e.g. find, partition, split) are often a very
          capable alternative (in combination with simple RE:s).  In your case,
          for JavaScript code that's as regular as the one in your example, you
          can split the string on "BlaObject( " and then use partition to strip off
          the first argument.
          >
          - only use RE:s to read specialized file formats if you know exactly
          what you're doing; there's often a ready-made library that does it much
          better.
          >
          - The regular expression machinery is not a parser.  You cannot handle
          all possible syntaxes with it, so don't even try.
          >
          </F>
          Thanks for the info, I will download that program later so I can build
          a re (i hope)

          Because I can't wait for that re, I have made a non re solution what
          is working for now.

          for a in bla.split():
          if a.find('BlaObje ct')<>-1:
          print a[11:].replace("'","" ).replace('"'," ").replace(",", "")

          (I know this is not the best way, but it helps me for now)

          Comment

          Working...