473,396 Members | 2,154 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,396 software developers and data experts.

can we use regular expression in split or strip function??

can we use regular expression in split or strip function??

i have a file as below

abc started on port 3458
status abc 255.255.255.345:5679 closed

i need to strip or split the above file wherever there are numericals in it ,as the port number keeps changing on running the scripts and it has to be compared with a sample file each time

is there any better way to achieve the same ??
Feb 13 '08 #1
2 1512
bvdet
2,851 Expert Mod 2GB
can we use regular expression in split or strip function??

i have a file as below

abc started on port 3458
status abc 255.255.255.345:5679 closed

i need to strip or split the above file wherever there are numericals in it ,as the port number keeps changing on running the scripts and it has to be compared with a sample file each time

is there any better way to achieve the same ??
You only want the port number? Maybe one of the following will work for you.
Expand|Select|Wrap|Line Numbers
  1. lineList = ['abc started on port 3458', 'status abc 255.255.255.345:5679 closed']
  2.  
  3. import re
  4.  
  5. patt = re.compile(r'port (\d+)')
  6. for item in lineList:
  7.     m = patt.search(item)
  8.     if m:
  9.         print m.group(1)
  10.  
  11. patt = re.compile(r'([0-9]+)$')
  12. for item in lineList:
  13.     for word in item.split():
  14.         m = patt.match(word)
  15.         if m:
  16.             print m.group(1)
  17.  
  18. for item in lineList:
  19.     if 'port' in item:
  20.         print item.split()[-1]
Feb 13 '08 #2
thanks :) ...i actually want to eliminate the port no. and have only the text either before or after the port number


You only want the port number? Maybe one of the following will work for you.
Expand|Select|Wrap|Line Numbers
  1. lineList = ['abc started on port 3458', 'status abc 255.255.255.345:5679 closed']
  2.  
  3. import re
  4.  
  5. patt = re.compile(r'port (\d+)')
  6. for item in lineList:
  7.     m = patt.search(item)
  8.     if m:
  9.         print m.group(1)
  10.  
  11. patt = re.compile(r'([0-9]+)$')
  12. for item in lineList:
  13.     for word in item.split():
  14.         m = patt.match(word)
  15.         if m:
  16.             print m.group(1)
  17.  
  18. for item in lineList:
  19.     if 'port' in item:
  20.         print item.split()[-1]
Feb 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average:...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
9
by: Schorschi | last post by:
Not having used regular expressions much, I need some help. Given a string... "This\0Guy\0Needs\0Some\0Help\0\0\0\0\0" Need result as array of strings... "This","Guy", "Needs", "Some", "Help" ...
4
by: Martin Biddiscombe | last post by:
It's probably quite simple, but what I want is a regular expression to parse strings of the form: "parameter=12ab" "parameter=12ab foo bar" "parameter='12ab'" "parameter='12ab' biz boz"...
5
by: Cylix | last post by:
I am going to write a function that the search engine done. in search engine, we may using double quotation to specify a pharse like "I love you", How can I using regular expression to sperate...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
9
by: micron_make | last post by:
I am trying to parse a file whose contents are : parameter=current max=5A min=2A for a single line I used for line in file: print re.search("parameter\s*=\s*(.*)",line).groups()
10
by: Julien | last post by:
Hi, I'm fairly new in Python and I haven't used the regular expressions enough to be able to achieve what I want. I'd like to select terms in a string, so I can then do a search in my database....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.