Regex Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    Regex Question

    I know almost nothing of Regular Expressions other than that they exist. However, I know that it's probably the answer to my co-worker's problem.

    We need to strip some HTML out of some data. The biggest problem we have is the <p> tags. But they include some other attributes. For example:
    Code:
    <p class="asdf1234">Some Text</p>
    <p class="qwer567890">Some Other Text</p>
    Our end goal is
    Code:
    Some Text
    Some Other Text
    We've so far gotten a regex to remove the closing </p>, and to get rid of an empty open <p>, but if it has any attributes included, the regex won't mach it.

    Can anyone suggest a regex that will match "<p" + any number of characters/symbols + ">" for me? I'd appreciate it.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I know nothing of RegEx either meaning my way is probably more brute force.
    Get the indexes of the "<" and ">" characters
    Discard everything before and including the first ">" and after and including the last "<"

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      insertAlias,

      In Python:
      Code:
      "<p.*>([^<>]+?)</p>"
      Example:
      Code:
      import re
      
      patt = re.compile(r"<p.*>([^<>]+?)</p>")
      
      def tag_text(s):
          output = []
          while True:
              m = patt.search(s)
              if m:
                  output.append(m.group(1))
                  s = s[m.end()+1:]
              else:
                  return output
      
      s = '''
      <p class="asdf1234">Some Text</p>
      <p class="qwer567890">Some Other Text</p>'''
      
      textList = tag_text(s)
      
      print textList
      Output:
      Code:
      >>> ['Some Text', 'Some Other Text']
      >>>

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by bvdet
        insertAlias,

        In Python:
        Code:
        "<p.*>([^<>]+?)</p>"
        The regex will largely be the same throughout languages.
        Last edited by NeoPa; Jan 6 '10, 12:39 AM. Reason: Fixed quote

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Code:
          </?p[^>]*>
          will match either the introduction or the termination.

          If you parse through your text changing this to blank then you should have what you need.

          < ==> Find string starting with <.
          /? ==> Next it may, or may not, have a /.
          p ==> A p must follow.
          [^>] ==> Any character other than >.
          * ==> Match any number of the preceding specification.
          > ==> A > must follow.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            I think Ade's is going to do it for me. I'll give it to my co-worker tomorrow and see if it works.

            Thanks for the response. I've been meaning to learn regex, but I'm a big slacker.

            Comment

            • MMcCarthy
              Recognized Expert MVP
              • Aug 2006
              • 14387

              #7
              IA if this answers the question (after consulting with your colleague) can you move this into misc. Otherwise it won't be public in searches.

              Thanks

              Mary

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                Sorry Mary. I should have done this before. There's no need to wait for the answer to be confirmed. That's where it should be anyway.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  BTW. I learnt all I know about RegExes from the Help section of a utility called TextPad. A pretty powerful text editor (I'm sure there are various other good ones out there too) which supports them. If you click Help while in the Search or Replace dialog boxes it takes you to three pages of info and details. Go through that and practice a bit (I found it so powerful I didn't need to try to practice) and you'll be making them dance in no time. It also acts as a reference when you can sort of remember what you need but need a memory jog.

                  There are probably other places more web available, but I only know this one well, as I used it to learn from and it did a good job for me.

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    I learned about regular expressions a little at a time. I found this page to be very informative and easy to understand. It is helpful having a way to easily test a regular expression. When you cannot figure out why an expression won't work, editing in a regex debugger makes it almost tolerable. I have been using Kodos.

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      The only problem is... where the hell is misc? It's disappeared from the navigation again.

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32633

                        #12
                        You could try the Ask Question link at the top.

                        It's not there either, but you could try just for fun :D

                        Otherwise the breadcrumbs is good from here :S

                        Comment

                        • dgreenhouse
                          Recognized Expert Contributor
                          • May 2008
                          • 250

                          #13
                          I know this is a 3 week old thread, but I'd like to note
                          that the following book is the RegEx bible:

                          Mastering Regular Expressions by Jeffrey E. F. Friedl
                          Publisher: O'Reilly (it's currently in its 3rd edition).

                          It sits to miy desk to the right; I've barely touched its depths; It hurts your head! :-)

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #14
                            That's going on my wishlist. Thanks, dgreenhouse.

                            Comment

                            Working...