(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="mystylesheet.css" rel="stylesheet" type="text/css">'
here it would be 'mystylesheet.css'. I used the following regex to get
this value(I dont know if it
is good).
_"<link\s+href=["]?(.*?)["]?\s+rel=["]?stylesheet["]?\s+type=["]?text/css["]?>"_
I thought I was doing fine until I got stuck by this tag >>
<link rel="stylesheet" href="mystylesheet.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="mystylesheet.css" type="text/css">
-OR-
<link href="mystylesheet.css" rel="stylesheet" type="text/css">
-OR-
<link type="text/css" href="mystylesheet.css" rel="stylesheet"> 5 1575
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*attribute1\s*)|(\s*attribute2\s*)|(\ s*attribute3\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="mystylesheet.css" rel="stylesheet" type="text/css">'
here it would be 'mystylesheet.css'. I used the following regex to get
this value(I dont know if it
is good).
_"<link\s+href=["]?(.*?)["]?\s+rel=["]?stylesheet["]?\s+type=["]?text/css["]?>"_
I thought I was doing fine until I got stuck by this tag >>
<link rel="stylesheet" href="mystylesheet.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="mystylesheet.css" type="text/css">
-OR-
<link href="mystylesheet.css" rel="stylesheet" type="text/css">
-OR-
<link type="text/css" href="mystylesheet.css" rel="stylesheet">
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="mystylesheet.css" rel="stylesheet" type="text/css">'
here it would be 'mystylesheet.css'. 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="mystylesheet.css" rel="stylesheet" type="text/css">' page=BeautifulSoup(html) page.link.get('href')
'mystylesheet.css'
--
- Justin
Justin Azoff wrote:
>from BeautifulSoup import BeautifulSoup html='<link href="mystylesheet.css" rel="stylesheet" type="text/css">' page=BeautifulSoup(html) page.link.get('href')
'mystylesheet.css'
On second thought, you will probably want something like
>>[link.get('href') for link in page.fetch('link',{'type':'text/css'})]
['mystylesheet.css']
which will properly handle multiple link tags.
--
- Justin
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="mystylesheet.css" type="text/css">
-OR-
<link href="mystylesheet.css" rel="stylesheet" type="text/css">
-OR-
<link type="text/css" href="mystylesheet.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.
Pyparsing is also good for recognizing basic HTML tags and their
attributes, regardless of the order of the attributes.
-- Paul
testText = """sldkjflsa;faj
<link href="mystylesheet.css" rel="stylesheet" type="text/css">
here it would be 'mystylesheet.css'. 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="mystylesheet.css" type="text/css" : same
tag but with 'href=' part
tags are like these? >>
<link rel="stylesheet" href="mystylesheet.css" type="text/css">
-OR-
<link href="mystylesheet.css" rel="stylesheet" type="text/css">
-OR-
<link type="text/css" href="mystylesheet.css" rel="stylesheet">
"""
from pyparsing import makeHTMLTags,line
linkTag = makeHTMLTags("link")[0]
for toks,s,e in linkTag.scanString(testText):
print toks.href
print line(s,testText)
print
Prints out:
mystylesheet.css
<link href="mystylesheet.css" rel="stylesheet" type="text/css">
mystylesheet.css
<link rel="stylesheet" href="mystylesheet.css" type="text/css" : same
mystylesheet.css
<link rel="stylesheet" href="mystylesheet.css" type="text/css">
mystylesheet.css
<link href="mystylesheet.css" rel="stylesheet" type="text/css">
mystylesheet.css
<link type="text/css" href="mystylesheet.css" rel="stylesheet"> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Harry |
last post by:
Hi there,
does anyone know how I can build a regular expression e.g. for the
string.search() function on runtime, depending on the content of
variables? Should be something like this:
var...
|
by: Dimitris Georgakopuolos |
last post by:
Hello,
I have a text file that I load up to a string. The text includes
certain expression like {firstName} or {userName} that I want to match
and then replace with a new expression. However,...
|
by: James D. Marshall |
last post by:
The issue at hand, I believe is my comprehension of using regular
expression, specially to assist in replacing the expression with other text.
using regular expression (\s*) my understanding is...
|
by: Billa |
last post by:
Hi,
I am replaceing a big string using different regular expressions (see
some example at the end of the message). The problem is whenever I
apply a "replace" it makes a new copy of string and I...
|
by: Pete Davis |
last post by:
I'm using regular expressions to extract some data and some links from some
web pages. I download the page and then I want to get a list of certain
links.
For building regular expressions, I use...
|
by: LordHog |
last post by:
Hello all,
I am attempting to create a small scripting application to be used
during testing. I extract the commands from the script file I was going
to tokenize the each line as one of the...
|
by: Mike |
last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in
matches. I would like to get what the actual regular expression is.
In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
|
by: shawnmkramer |
last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just
hanging and eventually getting a message "Requested Service not
found"?
I have the following pattern:
^(?<OrgCity>(+)+),...
|
by: sunil |
last post by:
Hi,
Am writing one C program for one of my module and facing one problem
with the regular expression functions provided by the library libgen.h
in solaris.
In this library we are having two...
|
by: Shawn B. |
last post by:
Greetings,
I'm using a custom WebBrowser control:
http://www.codeproject.com/KB/miscctrl/csEXWB.aspx
When I get the DocumentSource of a web page I browsed, and run a regular
expression...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |