473,750 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can this Perl regular expression be expressed in Python?

Here's a large Perl regular expression, from a Perl address parser in CPAN:

use re 'eval';
$Addr_Match{str eet} = qr/
(?:
# special case for addresses like 100 South Street
(?:($Addr_Match {direct})\W+ (?{ $_{street} = $^N })
($Addr_Match{ty pe})\b (?{ $_{type} = $^N }))
|
(?:($Addr_Match {direct})\W+ (?{ $_{prefix} = $^N }))?
(?:
([^,]+) (?{ $_{street} = $^N })
(?:[^\w,]+($Addr_Match{t ype})\b (?{ $_{type} = $^N }))
(?:[^\w,]+($Addr_Match{d irect})\b (?{ $_{suffix} = $^N }))?
|
([^,]*\d) (?{ $_{street} = $^N })
($Addr_Match{di rect})\b (?{ $_{suffix} = $^N })
|
([^,]+?) (?{ $_{street} = $^N })
(?:[^\w,]+($Addr_Match{t ype})\b (?{ $_{type} = $^N }))?
(?:[^\w,]+($Addr_Match{d irect})\b (?{ $_{suffix} = $^N }))?
)
)
/ix;

I'm trying to convert this to Python.

Those entries like "$(Addr_Match{d irect}) are other regular expressions,
being used here as subexpressions. Those have already been converted
to forms like "Addr_Match.dir ect" in Python. But how to call them?
Is that possible in Python, and if so, where is it documented?

John Nagle
Feb 14 '07 #1
3 1540
En Wed, 14 Feb 2007 01:07:33 -0300, John Nagle <na***@animats. com>
escribió:
Here's a large Perl regular expression, from a Perl address parser in
CPAN:

use re 'eval';
$Addr_Match{str eet} = qr/
(?:
# special case for addresses like 100 South Street
(?:($Addr_Match {direct})\W+ (?{ $_{street} = $^N })
($Addr_Match{ty pe})\b (?{ $_{type} = $^N }))
|
(?:($Addr_Match {direct})\W+ (?{ $_{prefix} = $^N }))?
(?:
([^,]+) (?{ $_{street} = $^N })
(?:[^\w,]+($Addr_Match{t ype})\b (?{ $_{type} = $^N }))
(?:[^\w,]+($Addr_Match{d irect})\b (?{ $_{suffix} = $^N }))?
|
([^,]*\d) (?{ $_{street} = $^N })
($Addr_Match{di rect})\b (?{ $_{suffix} = $^N })
|
([^,]+?) (?{ $_{street} = $^N })
(?:[^\w,]+($Addr_Match{t ype})\b (?{ $_{type} = $^N }))?
(?:[^\w,]+($Addr_Match{d irect})\b (?{ $_{suffix} = $^N }))?
)
)
/ix;

I'm trying to convert this to Python.

Those entries like "$(Addr_Match{d irect}) are other regular expressions,
being used here as subexpressions. Those have already been converted
to forms like "Addr_Match.dir ect" in Python. But how to call them?
Is that possible in Python, and if so, where is it documented?
That would be string interpolation, like this:

Addr_Match = {"direct": "some_re_string ",
"type": "other_re"
}

regexp = "%(direct)s %(type)s" % Addr_Match

--
Gabriel Genellina

Feb 14 '07 #2
Gabriel Genellina wrote:
En Wed, 14 Feb 2007 01:07:33 -0300, John Nagle <na***@animats. com>
escribió:
>Here's a large Perl regular expression, from a Perl address parser in
CPAN:

use re 'eval';
$Addr_Match{str eet} = qr/
(?:
# special case for addresses like 100 South Street
(?:($Addr_Match {direct})\W+ (?{ $_{street} = $^N })
($Addr_Match{ty pe})\b (?{ $_{type} = $^N }))
|
(?:($Addr_Match {direct})\W+ (?{ $_{prefix} = $^N }))?
(?:
([^,]+) (?{ $_{street} = $^N })
(?:[^\w,]+($Addr_Match{t ype})\b (?{ $_{type} = $^N }))
(?:[^\w,]+($Addr_Match{d irect})\b (?{ $_{suffix} = $^N
}))?
|
([^,]*\d) (?{ $_{street} = $^N })
($Addr_Match{di rect})\b (?{ $_{suffix} = $^N })
|
([^,]+?) (?{ $_{street} = $^N })
(?:[^\w,]+($Addr_Match{t ype})\b (?{ $_{type} = $^N }))?
(?:[^\w,]+($Addr_Match{d irect})\b (?{ $_{suffix} = $^N
}))?
)
)
/ix;

I'm trying to convert this to Python.

Those entries like "$(Addr_Match{d irect}) are other regular expressions,
being used here as subexpressions. Those have already been converted
to forms like "Addr_Match.dir ect" in Python. But how to call them?
Is that possible in Python, and if so, where is it documented?


That would be string interpolation, like this:

Addr_Match = {"direct": "some_re_string ",
"type": "other_re"
}

regexp = "%(direct)s %(type)s" % Addr_Match
You're right. I looked at the Perl code, and the strings are just being
inserted, not precompiled as regular expressions and called.

Incidentally, does anybody know what "$^N" means in Perl? That
abbreviation isn't in the list of special variables.

John Nagle
Feb 14 '07 #3
En Wed, 14 Feb 2007 04:11:37 -0300, John Nagle <na***@animats. com>
escribió:
Gabriel Genellina wrote:
>En Wed, 14 Feb 2007 01:07:33 -0300, John Nagle <na***@animats. com>
escribió:
>>Here's a large Perl regular expression, from a Perl address parser in
CPAN:

use re 'eval';
$Addr_Match{str eet} = qr/
(?:
# special case for addresses like 100 South Street
(?:($Addr_Match {direct})\W+ (?{ $_{street} = $^N
})
($Addr_Match{ty pe})\b (?{ $_{type} = $^N
}))
Incidentally, does anybody know what "$^N" means in Perl? That
abbreviation isn't in the list of special variables.
From the context it appears to be the "last matched group", or something
like that... but best look for some authoritative answer.

--
Gabriel Genellina

Feb 14 '07 #4

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

Similar topics

77
4055
by: Hunn E. Balsiche | last post by:
in term of its OO features, syntax consistencies, ease of use, and their development progress. I have not use python but heard about it quite often; and ruby, is it mature enough to be use for developing serious application, e.g web application as it has not many features in it yet. I've given up on Perl for its ugly syntax and it is not the easiest language to learn. How about PHP? Thanks
17
3104
by: Michael McGarry | last post by:
Hi, I am just starting to use Python. Does Python have all the regular expression features of Perl? Is Python missing any features available in Perl? Thanks, Michael
4
3879
by: Bill Chiu | last post by:
Hi: I'm used to perl's regular expression string matching, e.g. if ($line =~ /^(.*)+(.*)$/) { $name = $1; $age = $2; .... I want to do this in C++ using gcc under linux and cygwin - what would
9
4520
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our purposes. I have to admit that, although I'm very familiar with Python, I'm complete Perl noob (and I hope to stay one) which is reflected in my questions. I know that the web offers a lot of resources on Python/Perl differences. But I couldn't find a...
9
380
by: MJ | last post by:
HI I want to know what is mean by regular expression in C Mayur
6
4896
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
8
2721
by: John Pye | last post by:
Hi all I have a file with a bunch of perl regular expressions like so: /(^|)\*(.*?)\*(|$)/$1'''$2'''$3/ # bold /(^|)\_\_(.*?)\_\_(|$)/$1''<b>$2<\/ b>''$3/ # italic bold /(^|)\_(.*?)\_(|$)/$1''$2''$3/ # italic
3
2802
by: seberino | last post by:
How similar is Python's re module (regular expressions) compared to Perl's and grep's regular expression syntaxes? I really hope regular expression syntax is sufficiently standardized that we don't have to learn new dialects everytime we move from one language or shell command to another. chris
5
2923
by: prekida | last post by:
I have'nt used perl-style regular expression much. i have a need to search for a string called ORA-04031 error from a log file I use the following regular expression ORA-0*(4031) and the regular expression used does not find the search string in the log file The log file has the following contents
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.