473,320 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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="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">

Jul 13 '06 #1
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">
Jul 13 '06 #2
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

Jul 14 '06 #3
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

Jul 14 '06 #4
Ant
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.

Jul 14 '06 #5
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">

Jul 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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...
11
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,...
3
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...
7
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...
9
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...
3
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...
25
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...
5
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>(+)+),...
1
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...
1
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
0
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...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.