LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2005, 01:20 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Python - need help narrowing a regular expression


I'm doing some dual-posting to livejournal and to my own website, and I'm having trouble translating the <lj user> tags. Say I've got a string like this:
Code:
so...<lj user="user1"> and <lj user="user2"> came over to visit today.
And what I'd like to do is translate those <lj> tags into <a href> links, like so:
Code:
so...<a href="http://www.livejournal.com/users/user1">user1</a> and <a href="http://www.livejournal.com/users/user2">user2</a> came over to visit today.
My problem is that the regex I've been using matches the widest possible pattern, rather than the narrowest.
Code:
re.sub("lj user=\"(.*)\"", "a href=\"http://www.livejournal.com/users/\\1/\">\\1</a>", s)
where "s" is the string I described above. This gives user1"> and <lj user="user2 as the first group, rather than just user1, and needless to say that makes the HTML look really funny. Is there a supported flag which will let me tell Python to find the narrowest possible match, rather than the widest?

many thanks!
 
Old 10-29-2005, 08:19 AM   #2
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
I don't know python, but in perl the non-greedy version of (.*) is (.*?). You can also use a negated character class ([^\"]*).
 
Old 10-30-2005, 12:37 AM   #3
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
Quote:
Originally posted by Harmaa Kettu
I don't know python, but in perl the non-greedy version of (.*) is (.*?).
It's the same thing in python.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 07:48 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration