extracting lines matching keywords python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a text file in the following format:
Car: Replace:Brakes<10
Car: Renew: Engine=100000
Truck: Renew: Engine=1000
Truck: Replace: Brakes<504
I am looking to write a regex to parse this file and extract only the lines with Car in it and also only extract values after Car and return them as a python dictionary.
So my output would look like
'Replace' :' Brakes<10'
'Renew' : 'Engine=100000'
Any inputs on how I can achieve this?
I tried.
re.search
but get a re.Match object which I am not sure how to interpret.
Thank you!

There we go:
https://regex101.com/r/UTdN6B/1
use ^Car: (.*)|.* as pattern and \1 as substitute also gm as flags.

Related

Check if string follow a strict format via Regex Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have a string that might have any of the following format (example) :
1111__1111
1111__1111_11
111_11A_11
I have added the following check :
import re
print(bool(re.match("\d__\d","1111_1111"))
print(bool(re.match("\d__\d_\d","1111_1111_11"))
print(bool(re.match("\d_\d[A-Za-z]_\d","111_11A_11"))
I don't think the regex is correct because when I introduce a character in the first regex for example it returns me True Always.
can you please point me to a solution?
Thank you
It returns True because the pattern is trying to find matches based on each one of the characters inside the pattern string.
The following regular expression finds exact matches for the three scenarios:
print(bool(re.match("(^\d{4}__\d{4}$)","1111__1111")))
print(bool(re.match("(^\d{4}\_\d{4}\_\d{2}$)","1111_1111_11")))
print(bool(re.match("(^\d{3}_\d{2}[A-Z]_\d{2}$)","111_11A_11")))

get certain word from string that located between undercore, [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is one of the string that I got:
str ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
what I want:
['Name', 'coordinates','CITIZENS','colonisation']
I'm trying to get word in string such as Name, coordinate, citizens, colonisation with their original case.
I tried split method to remove underscores and make them individual word.
,but it did not work well.
How can I do this?
Yo can use a regular expression for that:
import re
text ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
re.findall('_(\w*)_', text)
Note str is a built python function, don't use for variable names
A regex should do the trick:
import re
s = '_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
result = re.findall('_(\w+)_', s)

Extract Data Enclosed between three asterisks in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want the data enclosed between three asterisks.And the Word should start with description.
For eg:I have data like
description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;
I want only
tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568
You may use re.findall here:
inp = "description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;"
matches = re.findall(r'\bdescription\s+\*{3}(.*?)\*{3}', inp, flags=re.DOTALL)
print(matches)
This prints:
['tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568']
Note that I use dot all mode in the regex, in case your expected matches might span across more than one line.

how to write regex to get only the users from this string output [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
consider below the output of my subprocess module of python. Now from here I want to grab the very first username only like root ,daemon and continuew. I tried but not able to write the exact regex to fetch the users only .
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
You do not a regex in the first place, as per your comments. So, you may iterate over the output line by line (str.splitlines()), split the line with : (str.split(':')) and take the first result (result[0]). This expects the output to be consistent, else it will fail.
Use split instead of regex
for line in output.split('\n'):
print line.split(':')[0]
>>>root
>>>daemon
....

Matching strings and extracting parts [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've an issue here. I have a string and I want to extract parts of it using regex. Here is the string
{{name}} I love me some work {{hero}}
I want to extract
[{{name}}, {{hero}}]
also in a case where the string exist as
{{name} I love me some work {{hero, come in {here, this is right}
I still want to get
[{{name}, {{name, {here, right}]
I hope this makes sense. I am working with Python.
If you want ['{{name}}', '{{hero}}', '{{hero, come in {here, this is right}'] use #Avinash's regex.
If you want ['{{name}}', '{{hero}}', '{{hero', {here', 'right}'] use the following:
re.findall(r'{+\w+}*|{*\w+}+', s)
RegEX DEMO
Have you tried the following?
import re
s = '{{name} I love me some work {{hero, come in {here, this is right}'
print re.findall(r'\{.*?\}', s)

Categories

Resources