substring and regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • borges2003xx@yahoo.it

    #1

    substring and regular expression

    i have read
    finding sublist



    the problem was in a string to find if we have two substring non
    overlapping of lenght al least 4

    it was done by
    r=re.compile(r' (?P<seq>.{4,}). *(?P=seq)')

    my knowhow on re is null
    there is a way in re for ask if a substring is the reverse of the
    otherone? First seq is second seq in reverse order

    i appreciate all persons that can help me.
    excuse my ignorance

  • Kent Johnson

    #2
    Re: substring and regular expression

    borges2003xx@ya hoo.it wrote:[color=blue]
    > i have read
    > finding sublist
    > http://groups.google.it/group/comp.l...56ada81fc9358a
    >
    >
    > the problem was in a string to find if we have two substring non
    > overlapping of lenght al least 4
    >
    > it was done by
    > r=re.compile(r' (?P<seq>.{4,}). *(?P=seq)')
    >
    > my knowhow on re is null
    > there is a way in re for ask if a substring is the reverse of the
    > otherone? First seq is second seq in reverse order[/color]

    For a fixed-length substring you can use brute force. Here is a regex that matches a string containing a four-character substring and its reverse:
    (?P<s1>.)(?P<s2 >.)(?P<s3>.)(?P <s4>.).*(?P=s4) (?P=s3)(?P=s2)( ?P=s1)

    You might want to look at the Regular Expression HOW-TO document:


    Kent

    Comment

    • borges2003xx@yahoo.it

      #3
      Re: substring and regular expression

      but in general is there a way to include in a re, in this example
      something like...matches iff p , and q in which p==q[::-1] ? A way to
      putting a small part of code of python in re? Thanx for your many helps

      Comment

      • Diez B.Roggisch

        #4
        Re: substring and regular expression

        borges2003xx <at> yahoo.it <borges2003xx <at> yahoo.it> writes:
        [color=blue]
        >
        > but in general is there a way to include in a re, in this example
        > something like...matches iff p , and q in which p==q[::-1] ? A way to
        > putting a small part of code of python in re? Thanx for your many helps[/color]

        What you are after is a parser - there are plenty available. I prefer spark, but
        pyparsing has gained lots of attention lately and is AFAIK the quasi-standard.

        The problem you described is a classical instance of a so-called
        context-free grammar.
        There is absolutely _no_ way to teach regular expressions how to detect words
        created based on a grammar of that kind. Really. It won't work.

        So - either use a parser, or write a simple one yourself - the code above
        qualifies as an attempt to do so.

        Regards,

        Diez

        Comment

        • borges2003xx@yahoo.it

          #5
          Re: substring and regular expression

          can you be so kind to show me a small example in spark if u like for
          remove all string with a substring of length 4 and the reverse of it?
          i thank you a lot

          Comment

          Working...