473,322 Members | 1,781 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,322 software developers and data experts.

help in converting perl re to python re

hi

i have some regular exp code in perl that i want to convert to python.
if $line =~ m#<(tag1)>(.*)</\1>#
{
$variable = $2;
}

the perl code finds a line that matches something like

"<tag1>sometext<\tag1>" in the line and then assign $variable the value
of "sometext"

how can i do an equivalent of that using re module?
thanks

Mar 3 '06 #1
4 1318
Hi

the perl code finds a line that matches something like
"<tag1>sometext<\tag1>" in the line and then assign $variable the value
of "sometext"
No, but if you use a closing </tag1> instead of <\tag1> it does. You had me scratching my head for a while there. :-)

This should do it in python:

------------------------------------------------
#!/usr/bin/python

import re

regexp = re.compile(r"<(tag1)>(.*)</\1>")
line = "<tag1>sometext</tag1>"
match = regexp.search(line)

if match:
variable = match.group(2)

------------------------------------------------

Good luck!
/Joel Hedlund
ei***********@yahoo.com wrote: hi

i have some regular exp code in perl that i want to convert to python.
if $line =~ m#<(tag1)>(.*)</\1>#
{
$variable = $2;
}

the perl code finds a line that matches something like

"<tag1>sometext<\tag1>" in the line and then assign $variable the value
of "sometext"

how can i do an equivalent of that using re module?
thanks

Mar 3 '06 #2
Joel Hedlund enlightened us with:
regexp = re.compile(r"<(tag1)>(.*)</\1>")


I'd go for

regexp = re.compile(r"<(tag1)>(.*?)</\1>")

Otherwise this:

line = "<tag1>sometext</tag1><tag1>othertext</tag1>"
match = regexp.search(line)

will result in 'sometext</tag1><tag1>othertext'

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Mar 3 '06 #3
> I'd go for
regexp = re.compile(r"<(tag1)>(.*?)</\1>")


Indeed. I second that.

/Joel
Mar 3 '06 #4
>> i have some regular exp code in perl that i want to convert to python.
if $line =~ m#<(tag1)>(.*)</\1>#
{
$variable = $2;
}
regexp = re.compile(r"<(tag1)>(.*)</\1>")
line = "<tag1>sometext</tag1>"
match = regexp.search(line)
if match:
variable = match.group(2)


Or, if you prefer shorter, quick-and-dirty syntax closer to perl's approach:

match = re.search(r"<(tag1)>(.*)</\1>", line)
if match: variable = match.group(2)
Mar 3 '06 #5

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

Similar topics

42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
2
by: Tomislav Lepusic | last post by:
Hello, I don't know if this is the right group (I'm more in Perl, know nothing about Python), so if you can help me thanks, if not, sorry to bother you. I'm working on my student project and...
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
5
by: Mothra | last post by:
Hi All, I am the current author of the Astro-Sunrise perl module http://search.cpan.org/~rkhill/Astro-Sunrise-0.91/Sunrise.pm and was wondering if it would be worth while to convert it to...
7
by: ChiTownBob | last post by:
I'm trying to convert our documentation over to the Wikipedia format (MediaWiki), and about ready to give up on the Perl-based method I found (because Perl just sucks, as all good Python hackers...
4
by: eight02645999 | last post by:
hi i need help with converting a piece of perl code to python ...... my $start = '\'; my $file = '\'; my $end = '\'; ..... while(<FILE>) #open a file {
13
by: squash | last post by:
I am a little annoyed at why such a simple program in Perl is causing so much difficulty for python, i.e: $a += 200000 * 140000; print $a;
10
by: stylecomputers | last post by:
Hey guys, I am absolutely new to Linux programming, with no w######s programming experience except a small amount of C++ console apps. Reasonably new to Linux, BSD etc, got good sound networking...
4
by: pmcgover | last post by:
I enjoyed Paul Barry's September article in Linux Journal entitled, "Web Reporting with MySQL, CSS and Perl". It provides a simple, elegant way to use HTML to display database content without any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: 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...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.