473,790 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regexp in Python (from Perl)

Pat
I have a regexp in Perl that converts the last digit of an ip address to
'9'. This is a very particular case so I don't want to go off on a
tangent of IP octets.

( my $s = $str ) =~ s/((\d+\.){3})\d+/${1}9/ ;

While I can do this in Python which accomplishes the same thing:

ip = ip[ :-1 ]
ip =+ '9'

I'm more interested, for my own edification in non-trivial cases, in how
one would convert the Perl RE to a Python RE that use groups. I am
somewhat familiar using the group method from the re package but I
wanted to know if there was a one-line solution.

Thank you.
Oct 19 '08 #1
6 2986
On Oct 19, 5:47*pm, Bruno Desthuilliers
<bdesth.quelque ch...@free.quel quepart.frwrote :
Pat a écrit :
I have a regexp in Perl that converts the last digit of an ip address to
*'9'. *This is a very particular case so I don't want to go off on a
tangent of IP octets.
*( my $s = $str ) =~ s/((\d+\.){3})\d+/${1}9/ ;
While I can do this in Python which accomplishes the same thing:
ip = ip[ :-1 ]
ip =+ '9'

or:

ip = ip[:-1]+"9"
I'm more interested, for my own edification in non-trivial cases, in how
one would convert the Perl RE to a Python RE that use groups. *I am
somewhat familiar using the group method from the re package but I
wanted to know if there was a one-line solution.

Is that what you want ?

*>>re.sub(r'^(( (\d+)\.){3})\d+ $', "\g<1>9", "192.168.1. 1")
'192.168.1.9'
>re.sub(r'^(((\ d+)\.){3})\d+$' , "\g<1>9", "192.168.1.100" )

'192.168.1.9'
The regular expression changes the last sequence of digits to
"9" ("192.168.1.100 " ="192.168.1. 9") but the other code replaces the
last digit ("192.168.1.100 " ="192.168.1.109 ").
Oct 19 '08 #2
MRAB:
The regular expression changes the last sequence of digits to
"9" ("192.168.1.100 " ="192.168.1. 9") but the other code replaces the
last digit ("192.168.1.100 " ="192.168.1.109 ").
Uhmm, this is a possible alternative:
>>s = " 192.168.1.100 "
".".join(s.st rip().split("." )[:3]) + ".9"
'192.168.1.9'

Bye,
bearophile
Oct 19 '08 #3
MRAB a écrit :
On Oct 19, 5:47 pm, Bruno Desthuilliers
<bdesth.quelque ch...@free.quel quepart.frwrote :
>Pat a écrit :
(snip)
>>ip = ip[ :-1 ]
ip =+ '9'
or:

ip = ip[:-1]+"9"
(snip)
> >>re.sub(r'^((( \d+)\.){3})\d+$ ', "\g<1>9", "192.168.1. 1")
'192.168.1.9 '
>>>>re.sub(r'^( ((\d+)\.){3})\d +$', "\g<1>9", "192.168.1.100" )
'192.168.1.9 '

The regular expression changes the last sequence of digits to
"9" ("192.168.1.100 " ="192.168.1. 9") but the other code replaces the
last digit ("192.168.1.100 " ="192.168.1.109 ").
Mmm - yes, true.

ip = ".".join(ip.spl it('.')[0:3] + ['9'])
Oct 20 '08 #4
Pat
Bruno Desthuilliers wrote:
Pat a écrit :
>I have a regexp in Perl that converts the last digit of an ip address
to '9'. This is a very particular case so I don't want to go off on
a tangent of IP octets.

( my $s = $str ) =~ s/((\d+\.){3})\d+/${1}9/ ;

While I can do this in Python which accomplishes the same thing:

ip = ip[ :-1 ]
ip =+ '9'

or:

ip = ip[:-1]+"9"
Yes! That's exactly what I was looking for.
>
>I'm more interested, for my own edification in non-trivial cases, in
how one would convert the Perl RE to a Python RE that use groups. I
am somewhat familiar using the group method from the re package but I
wanted to know if there was a one-line solution.

Is that what you want ?
>>re.sub(r'^((( \d+)\.){3})\d+$ ', "\g<1>9", "192.168.1. 1")
'192.168.1.9'
>>>re.sub(r'^(( (\d+)\.){3})\d+ $', "\g<1>9", "192.168.1.100" )
'192.168.1.9'

Ah-hah! That's how one uses groups. It's beautiful. I couldn't find
that in my books. Thank you very, very much!

At first, I thought that using RE's in Python was going to be more
difficult than Perl. A lot of my Perl code makes heavy use of RE
searching and substitution.

I will never, ever write another line of Perl code as long as I live.
Oct 20 '08 #5
Pat a écrit :
Bruno Desthuilliers wrote:
>Pat a écrit :
>>I have a regexp in Perl that converts the last digit of an ip address
to '9'. This is a very particular case so I don't want to go off on
a tangent of IP octets.

( my $s = $str ) =~ s/((\d+\.){3})\d+/${1}9/ ;

While I can do this in Python which accomplishes the same thing:

ip = ip[ :-1 ]
ip =+ '9'

or:

ip = ip[:-1]+"9"

Yes! That's exactly what I was looking for.
Perhaps not - cf MRAB's post earlier in this thread and bearophile's answer.

While this was a straightforward one-liner version of your own code, it
doesn't behave like the re version : this one only replace the last
_character_, while the re version replaces the last _group_. If you want
the re version's behaviour, use this instead:

ip = ".".join(ip.spl it('.')[0:3] + ['9'])

>>
>>I'm more interested, for my own edification in non-trivial cases, in
how one would convert the Perl RE to a Python RE that use groups. I
am somewhat familiar using the group method from the re package but I
wanted to know if there was a one-line solution.

Is that what you want ?
> >>re.sub(r'^((( \d+)\.){3})\d+$ ', "\g<1>9", "192.168.1. 1")
'192.168.1.9 '
>>>>re.sub(r'^( ((\d+)\.){3})\d +$', "\g<1>9", "192.168.1.100" )
'192.168.1.9 '


Ah-hah! That's how one uses groups. It's beautiful. I couldn't find
that in my books.
Did you look at the FineManual(tm) ?-)
>
At first, I thought that using RE's in Python was going to be more
difficult than Perl. A lot of my Perl code makes heavy use of RE
searching and substitution.
Well... Python's re module makes for a bit more verbose code, and I'm
not sure all of the perl's regexps features are implemented. But that's
still quite enough to shoot yourself in the foot IMHO !-)

Now while regexps are a must-have in a programmer's toolbox, you can
already do quite a few things just using slicing and string methods. I
highly recommend you read the relevant doc.
I will never, ever write another line of Perl code as long as I live.
Hmmm... Never say "never again" ?-)
Oct 20 '08 #6
Pat
Bruno Desthuilliers wrote:
MRAB a écrit :
>On Oct 19, 5:47 pm, Bruno Desthuilliers
<bdesth.quelqu ech...@free.que lquepart.frwrot e:
>>Pat a écrit :
(snip)
>>>ip = ip[ :-1 ]
ip =+ '9'
or:

ip = ip[:-1]+"9"
(snip)
>> >>re.sub(r'^((( \d+)\.){3})\d+$ ', "\g<1>9", "192.168.1. 1")
'192.168.1. 9'

>re.sub(r'^ (((\d+)\.){3})\ d+$', "\g<1>9", "192.168.1.100" )
'192.168.1. 9'

The regular expression changes the last sequence of digits to
"9" ("192.168.1.100 " ="192.168.1. 9") but the other code replaces the
last digit ("192.168.1.100 " ="192.168.1.109 ").

Mmm - yes, true.

ip = ".".join(ip.spl it('.')[0:3] + ['9'])
As I first stated, in my very particular case, I knew that the last
octet was always going to be a single digit.

But I did learn a lot from everyone else's posts for the more generic
cases. thx!
Oct 24 '08 #7

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

Similar topics

1
3034
by: bezeee | last post by:
At my work we are in the process of building a tool to test an XML based API. Basically, XML in and XML out over http. Currently, there are two engines that do all of the schema validations, xml diffs, sending/receiving, etc. One of these engines in implemented in C# and the other in Java. Now the choice comes down to which scripting language we choose (Perl, Python or Jython) to tie into one of these engines. The scripting language...
9
2144
by: Roy Smith | last post by:
I'm working on a prototype of a new application in Python. At some point, if this ever turns into a product, the powers that be will almost certainly demand that it be done in Perl. My job will be to convince them otherwise. The basic design of the application is object oriented. I've never used Perl's OO features, so I'm not in a good position to make a comparison of the two languages from an OO point of view. Can somebody who's...
2
4445
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some sorted columns. to do this at the moment i am using perl, which is very easy to do, and i want to see if python is as easy. so, the data i am using is some epiphyte population abundance data for particular sites, and it looks like this:
5
2454
by: Premshree Pillai | last post by:
Hello, I recently wrote a Perl version of pyAlbum.py -- a Python script to create an image album from a given directory -- plAlbum.pl . It made me realize how easy-to-use Python is. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/271246
17
3111
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
31
4811
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 great for pattern matching, text processing, and automated testing. Our company is really fixated on risk managnemt and the only way I can do enough testing without working overtime (which some people have ended up doing) is by automating my...
9
4526
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...
0
796
by: Chris | last post by:
I have an old application that has an embedded Perl interpreter exposing an API. However, all the code I have needs to use the API is in Python. What's the easiest way access my Python code from inside Perl? The closest thing I've found is the Inline::Python module (http://search.cpan.org/~neilw/Inline-Python-0.22/Python.pod), but it seems to have died about 6 years ago.
8
2437
by: Palindrom | last post by:
Hi everyone ! I'd like to apologize in advance for my bad english, it's not my mother tongue... My girlfriend (who is a newbie in Python, but knows Perl quite well) asked me this morning why the following code snippets didn't give the same result : ### Python ###
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10200
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
10145
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
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
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...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
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
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.