Regular Expression problem

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

    #1

    Regular Expression problem

    (I don't know if it is the right place. So if I am wrong, please point
    me the right direction.
    If this post is read by you masters, I'm honoured. If I am getting a
    mere response, I'm blessed!)

    Hi,

    I'm a newbie regular expression user. I use regex in my Python
    programs. I have a strange

    (sometimes not strange, but please bear in mind; I'm a newbie ;)
    problem using regex. That I want

    a particular tag value of one of my HTML files.

    ie: I want only the value after 'href=' in the tag >>

    '<link href="mystylesh eet.css" rel="stylesheet " type="text/css">'

    here it would be 'mystylesheet.c ss'. I used the following regex to get
    this value(I dont know if it

    is good).

    _"<link\s+hr ef=["]?(.*?)["]?\s+rel=["]?stylesheet["]?\s+type=["]?text/css["]?>"_
    I thought I was doing fine until I got stuck by this tag >>

    <link rel="stylesheet " href="mystylesh eet.css" type="text/css" : same
    tag but with 'href=' part

    at a different place. I think you got the point!

    So What should I do to get the exact value(here the value after
    'href=') in any case even if the

    tags are like these? >>

    <link rel="stylesheet " href="mystylesh eet.css" type="text/css">
    -OR-
    <link href="mystylesh eet.css" rel="stylesheet " type="text/css">
    -OR-
    <link type="text/css" href="mystylesh eet.css" rel="stylesheet ">

  • cdecarlo

    #2
    Re: Regular Expression problem

    Hey,

    I'm new with regex's as well but here is my idea. Since you don't know
    which attribute will come first why don't structure your regex like
    this

    (first off, I'll assume that \s == ' ', actually now that I think of
    it, isn't \s any whitespace character? anyways \s == ' ' for now)

    '<link\s*((\s*a ttribute1\s*)|( \s*attribute2\s *)|(\s*attribut e3\s*))+>'

    I think that should just about do it.

    Hope this helped,

    Colin

    John Blogger wrote:
    (I don't know if it is the right place. So if I am wrong, please point
    me the right direction.
    If this post is read by you masters, I'm honoured. If I am getting a
    mere response, I'm blessed!)
    >
    Hi,
    >
    I'm a newbie regular expression user. I use regex in my Python
    programs. I have a strange
    >
    (sometimes not strange, but please bear in mind; I'm a newbie ;)
    problem using regex. That I want
    >
    a particular tag value of one of my HTML files.
    >
    ie: I want only the value after 'href=' in the tag >>
    >
    '<link href="mystylesh eet.css" rel="stylesheet " type="text/css">'
    >
    here it would be 'mystylesheet.c ss'. I used the following regex to get
    this value(I dont know if it
    >
    is good).
    >
    _"<link\s+hr ef=["]?(.*?)["]?\s+rel=["]?stylesheet["]?\s+type=["]?text/css["]?>"_
    I thought I was doing fine until I got stuck by this tag >>
    >
    <link rel="stylesheet " href="mystylesh eet.css" type="text/css" : same
    tag but with 'href=' part
    >
    at a different place. I think you got the point!
    >
    So What should I do to get the exact value(here the value after
    'href=') in any case even if the
    >
    tags are like these? >>
    >
    <link rel="stylesheet " href="mystylesh eet.css" type="text/css">
    -OR-
    <link href="mystylesh eet.css" rel="stylesheet " type="text/css">
    -OR-
    <link type="text/css" href="mystylesh eet.css" rel="stylesheet ">

    Comment

    • Justin  Azoff

      #3
      Re: Regular Expression problem

      John Blogger wrote:
      That I want a particular tag value of one of my HTML files.
      >
      ie: I want only the value after 'href=' in the tag >>
      >
      '<link href="mystylesh eet.css" rel="stylesheet " type="text/css">'
      >
      here it would be 'mystylesheet.c ss'. I used the following regex to get
      this value(I dont know if it is good).
      No matter how good it is you should still use something that
      understands html:
      >>from BeautifulSoup import BeautifulSoup
      >>html='<link href="mystylesh eet.css" rel="stylesheet " type="text/css">'
      >>page=Beautifu lSoup(html)
      >>page.link.get ('href')
      'mystylesheet.c ss'

      --
      - Justin

      Comment

      • Justin  Azoff

        #4
        Re: Regular Expression problem

        Justin Azoff wrote:
        >from BeautifulSoup import BeautifulSoup
        >html='<link href="mystylesh eet.css" rel="stylesheet " type="text/css">'
        >page=Beautiful Soup(html)
        >page.link.get( 'href')
        'mystylesheet.c ss'
        On second thought, you will probably want something like
        >>[link.get('href' ) for link in page.fetch('lin k',{'type':'tex t/css'})]
        ['mystylesheet.c ss']

        which will properly handle multiple link tags.

        --
        - Justin

        Comment

        • Ant

          #5
          Re: Regular Expression problem

          So What should I do to get the exact value(here the value after
          'href=') in any case even if the
          >
          tags are like these? >>
          >
          <link rel="stylesheet " href="mystylesh eet.css" type="text/css">
          -OR-
          <link href="mystylesh eet.css" rel="stylesheet " type="text/css">
          -OR-
          <link type="text/css" href="mystylesh eet.css" rel="stylesheet ">
          The following should do it:

          expr = r'<link .*?href="(.*?)" '

          or if single quotes might have been used:

          expr = r'''<link .*?href=["'](.*?)['"]'''

          But like the others have said, beautiful soup is very good for things
          like this.

          Comment

          • Paul McGuire

            #6
            Re: Regular Expression problem

            Pyparsing is also good for recognizing basic HTML tags and their
            attributes, regardless of the order of the attributes.

            -- Paul

            testText = """sldkjflsa;fa j

            <link href="mystylesh eet.css" rel="stylesheet " type="text/css">

            here it would be 'mystylesheet.c ss'. I used the following regex to get
            this value(I dont know if it

            I thought I was doing fine until I got stuck by this tag >>

            <link rel="stylesheet " href="mystylesh eet.css" type="text/css" : same

            tag but with 'href=' part

            tags are like these? >>

            <link rel="stylesheet " href="mystylesh eet.css" type="text/css">
            -OR-
            <link href="mystylesh eet.css" rel="stylesheet " type="text/css">
            -OR-
            <link type="text/css" href="mystylesh eet.css" rel="stylesheet ">

            """
            from pyparsing import makeHTMLTags,li ne

            linkTag = makeHTMLTags("l ink")[0]
            for toks,s,e in linkTag.scanStr ing(testText):
            print toks.href
            print line(s,testText )
            print

            Prints out:

            mystylesheet.cs s
            <link href="mystylesh eet.css" rel="stylesheet " type="text/css">

            mystylesheet.cs s
            <link rel="stylesheet " href="mystylesh eet.css" type="text/css" : same


            mystylesheet.cs s
            <link rel="stylesheet " href="mystylesh eet.css" type="text/css">

            mystylesheet.cs s
            <link href="mystylesh eet.css" rel="stylesheet " type="text/css">

            mystylesheet.cs s
            <link type="text/css" href="mystylesh eet.css" rel="stylesheet ">

            Comment

            Working...