Sponsored Content
Top Forums Shell Programming and Scripting Python Regex Removing One Too Many... Post 302890870 by Azrael on Saturday 1st of March 2014 12:19:07 PM
Old 03-01-2014
Python Regex Removing One Too Many...

Well, I'm a python noob and my last post here I was introduced to Regex. I thought this would be easy since I knew Regex with Bash. However, I've been banging my head a while to extract an ip address from ifconfig with this:

Code:
#!/usr/bin/python

import re
import subprocess
from subprocess import Popen, PIPE

pattern = re.compile(r'inet.\S+(\d+\.\d+\.\d+\.\d+)')

p1 = Popen(["/sbin/ifconfig"], stdout=subprocess.PIPE)

output = p1.communicate()[0]
result = re.findall(pattern, output)

print result[1]

Unfortunately I'm getting one too characters cut out. I should get 10.0.0.139 instead of the following:

Code:
$ ./saturday.py 
0.0.0.139

Anyone see where I went wrong?
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python Regex

I have the below string and regex. However I cant understand why it works the way it does. IP has been changed for safety ;) String = NowSMS Error Report. Error initializing SMSC Interface 'SMPP - 10.15.8.10:17600'. Interface is not available. Regex = (.+\.)\s(.+) I get two... (1 Reply)
Discussion started by: barney34
1 Replies

2. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

3. Shell Programming and Scripting

Python Newbie Question Regex

I starting teaching myself python and am stuck on trying to understand why I am not getting the output that I want. Long story short, I am using PDB for debugging and here my function in which I am having my issue: import re ... ... ... def find_all_flvs(url): soup =... (1 Reply)
Discussion started by: metallica1973
1 Replies

4. Programming

Python Reading Individual files and Regex through them

As a newbie to Python, I am trying to write a script in which is will add all the log files (*.log) from within a directory to a list, open the files and search for an ip using a regex and single it out (appending the ip's to the list). So far, I have: import re, os def list_files() content = ... (4 Replies)
Discussion started by: metallica1973
4 Replies

5. Shell Programming and Scripting

How to use regex on particular column (Removing comma from particular column)?

Hi, I have pipe separated file which contains some data having comma(,) in it. I want to remove the comma(,) only from particular column without changing data in other columns. Below is the sample data file, I want to remove the comma(,) only from 5th column. $ cat file1 ABC | DEF, HIJ|... (6 Replies)
Discussion started by: Prathmesh
6 Replies

6. Programming

Python Regex List Creation

Here is a snippet of my code: blahblahblah... blah for link in goodies.soup.find_all('a'): blah.append(link.get('href')) blah=list(set(blah)) which gives my list of urls. So now I use a regex to search for the relevant urls which I want in a list: for r... (0 Replies)
Discussion started by: metallica1973
0 Replies

7. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

8. Shell Programming and Scripting

Python with Regex and Excel

Hello I have a big excel file for Ticket Data Analysis. The idea is to make meaningful insight from Resolution Field. Now as people write whatever they feel like while resolving the ticket it makes quite a task. 1. They may or may not tag it with something like below within the resolution... (1 Reply)
Discussion started by: radioactive9
1 Replies

9. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies
CGI::Application::Plugin::ActionDispatch(3pm)		User Contributed Perl Documentation	     CGI::Application::Plugin::ActionDispatch(3pm)

NAME
CGI::Application::Plugin::ActionDispatch - Perl extension SYNOPSIS
# In "WebApp.pm"... package WebApp; use base 'CGI::Application'; use CGI::Application::Plugin::ActionDispatch; sub do_stuff : Path('do/stuff') { ... } sub do_more_stuff : Regex('^/do/more/stuff/?$') { ... } sub do_something_else : Regex('do/something/else/(w+)/(d+)$') { ... } DESCRIPTION
CGI::Application::Plugin::ActionDispatch adds attribute based support for parsing the PATH_INFO of the incoming request. For those who are familiar with Catalyst. The interface works very similar. This plugin is plug and play and shouldn't interrupt the default behavior of CGI::Application. CAVEATS
Be aware though, this plugin will not likely work with other modules that use attributes. This module should work with mod_perl. It however has not be thoroughly tested as such. If you have used it with mod_perl please e-mail me with your experience. METHODS
action_args() If using capturing parentheses in a Regex action. The captured values are accessible using this method. sub addElement : Regex('add/(d+)/(d+)') { my $self = shift; my($column, $row) = $self->action_args(); ... } The Path action will store everything after the matched path into the action args. # http://example.com/state/pa/philadelphia sub find_state_and_city : Path('state/') { my $self = shift; my($state, $city) = $self->action_args(); # $state == pa, $city == philadelphia ... } ACTIONS
Regex Regex action is used for regular expression matching against PATH_INFO. If capturing parentheses are used; the matched parameters are accesssible using the action_args() method. Regex('^blah/foo'); The Regex action either matches or it doesn't. There are no secrets to it. It is important to note Regex action takes priority. It is assumed if a Path and Regex action both match. The Regex action will take priority, which may not always be the outcome of least suprise, for instance: # http://example.com/music/the_clash sub clash : Path('/music/the_clash') {} # This is an exact match, BUT. sub the_class : Regex('/music/the_clash') {} # This takes priority. Beware. Path The Path action is basically a shortcut for a commonly used Regex action. # http://example.com/products/movies/2 sub show_product : Path('products/') { my $self = shift; my($category, $id) = $self->action_args(); .... } Is basically the same thing as. sub show_product : Regex('^/products/(w+)/(d+)') { my $self = shift; my($category, $id) = $self->action_args(); ... } For those that care, the Path('products/') will be converted to the regular expression "^/products/?(/.*)$"; then split('/') is run on the captured value and stored in action_args(). Runmode This action will take the method name and run a match on that. # http://example.com/foobar sub foobar : Runmode {} Default The default run mode if no match is found. Essentially the equivalent of the start_mode() method. sub default_mode : Default {} EXAMPLE
In CGI::Application module: package WebApp; use base 'CGI::Application'; use CGI::Application::Plugin::ActionDispatch; use strict; sub setup { my $self = shift; self->mode_param('test_rm'); $self->run_modes( basic_runmode => 'basic_runmode' ); } # Regular runmodes should work. sub basic_runmode { my $self = shift } The product() runmode will match anything starting with "/products" in the PATH_INFO. # http://example.com/myapp.cgi/products/this/is/optional/and/stored/in/action_args/ sub product : Path('products/') { my $self = shift; my($category, $product) = $self->action_args(); } The music() runmode will match anything starting with "/products/music" in the PATH_INFO. The product() runmode also matches "/products/music". However since this runmode matches closer it takes priority over product(). # http://example.com/myapp.cgi/products/music/product/ sub music : Path('products/music/') { my $self = shift; my $product = $self->action_args(); ... } This beatles() runmode will match ONLY "/product/music/beatles" or "/product/music/beatles/". Regex takes priority over Path so the previous runmodes which match this PATH_INFO are not run. # http://example.com/myapp.cgi/products/music/beatles/ sub beatles : Regex('^/products/music/beatles/?') { my $self = shift; ... } SEE ALSO
CGI::Application, CGI::Application::Dispatch http://github.com/jaywhy/cgi-application-plugin-actiondispatch AUTHOR
Jason Yates, <jaywhy@gmail.com> COPYRIGHT AND LICENSE
Copyright (C) 2006-2008 by Jason Yates This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available. perl v5.12.4 2011-10-26 CGI::Application::Plugin::ActionDispatch(3pm)
All times are GMT -4. The time now is 11:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy