Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract Specific Pattern
#1
Hi Team,

How do I extract specific pattern in below code. As I have tried to get expected output through regex and use Python Dictionary in order to fetch it through values but unable to get it.


#!/usr/bin/python
import re


line = '''CLIENT LIST
Updated,Wed Sep 19 10:22:48 2018
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
dell,110.23.15.12:50731,14298,15058,Wed Sep 19 09:59:23 2018
sony,110.23.15.12:48050,93394,98996,Wed Sep 19 10:21:44 2018
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
192.168.250.114,dell,110.23.15.12:48050,Wed Sep 19 10:22:46 2018
192.168.250.50,sony,110.23.15.12:50731,Wed Sep 19 09:59:25 2018
GLOBAL STATS
Max bcast/mcast queue length,0
END'''

dict = re.findall(r'^\d(.*?,\s*(.*?),)', line, re.M)

if dict:
 print "Users Connected", dict
else:
 print "Nothing found!!"
Output:
Output:
Users Connected: [('92.168.250.114,dell,', 'dell'), ('92.168.250.50,sony,', 'sony')]
Expected Output:
Output:
Users Connected: dell, sony and so on...
Regards,
rlinux57
Reply
#2
That looks more like "desired output" than "expected output" - the string "Users Connected" never appears in your code. Also, there are no dictionaries here, why do you think there are?

If I wanted to produce your desired result from the code you've provided, I'd simplify the regex, since it looks like you don't need the IP address (from what you've described) and then I would use the list of strings from findall to generate the desired string. I'm not sure what your programming background is, but that would probably help us to tailor a specific answer to you.
Reply
#3
Hi,

I have edited and print the "Users Connected" to dispel your confusion. I just want to get names through regex. But i am bit weak in regex to extract desired output. When i used python dictionary like dict[0][1] It shows only single value. I am not sure how to give a range so that it shows all the values stored in dict variable.
Reply
#4
(Sep-25-2018, 05:12 AM)rlinux57 Wrote: Hi,

I have edited and print the "Users Connected" to dispel your confusion. I just want to get names through regex. But i am bit weak in regex to extract desired output. When i used python dictionary like dict[0][1] It shows only single value. I am not sure how to give a range so that it shows all the values stored in dict variable.

Learn non-capturing group

Output:
In [44]: re.findall(r'(?m)^(?:[\d\.]+,)(\w+)', line) Out[44]: ['dell', 'sony']
PS Never use names of Python built-in objects as variable names
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
Hi Volcano63,

Thank you for your answer, I used: ^\d[0-9]+(?:\.[0-9]+){3},(\w+)

Regards,
rlinux57
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using locationtagger to extract locations found in a specific country/region lord_of_cinder 1 1,296 Oct-04-2022, 12:46 AM
Last Post: Larz60+
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,292 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extract text based on postion and pattern guddu_12 2 1,657 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  regex pattern to extract relevant sentences Bubly 2 1,888 Jul-06-2021, 04:17 PM
Last Post: Bubly
  Extract specific sentences from text file Bubly 3 3,440 May-31-2021, 06:55 PM
Last Post: Larz60+
  How to extract specific key value pair from string? aditi06 0 2,554 Apr-15-2021, 06:26 PM
Last Post: aditi06
  Remove specific elements from list with a pattern Xalagy 3 2,737 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  How to extract specific rows and columns from a text file with Python Farhan 0 3,410 Mar-25-2020, 09:18 PM
Last Post: Farhan
  extract specific data from a group of json-files ledgreve 3 3,313 Dec-05-2019, 07:57 PM
Last Post: ndc85430
  Delete specific lines contain specific words mannyi 2 4,165 Nov-04-2019, 04:50 PM
Last Post: mannyi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020