Regular expression operations in Python [closed] - python

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 3 years ago.
Improve this question
How to get what I want? For example, I have a string like this
'RC00001 C00003_C00004RC00087 C00756_C01545RC01045 C06756_C03485'
I want to get
'RC00001 C00003_C00004','RC00087 C00756_C01545','RC01045 C06756_C03485'
What should I do? I have tried many times, but I failed. Please help me! Thank you!

answer=[]
a="RC00001 C00003_C00004RC00087 C00756_C01545RC01045 C06756_C03485"
b = a.split("RC")
for i in b[1:]:
answer.append("RC%s" % (i))
print(answer)
This will output:
['RC00001 C00003_C00004', 'RC00087 C00756_C01545', 'RC01045 C06756_C03485']

If you want to achieve this using regex, you could try the following
import re
input_str = 'RC00001 C00003_C00004RC00087 C00756_C01545RC01045 C06756_C03485'
pattern = '(RC[\d+]+\s+C[\d]+_C[\d]+)'
print(re.findall(pattern, input_str))
# output
# [('RC00001 C00003_C00004', 'RC00087 C00756_C01545', 'RC01045 C06756_C03485')]
provided the format is always RC{numbers} C{numbers}

Related

Splitting a line of complex string [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 3 years ago.
Improve this question
I need to split a really complex line for me. The line I want to split is as follows
2019.10.20-22.01.33: '10.11.111.25 9999995555884411:TechnoBeceT(69)' logged in
how can i split this like this
['2019.10.20-22.01.33', '10.11.111.25', '9999995555884411', 'logged in']
i don't need
TechnoBeceT(69) this area.
Using Regular Expression
import re
p = re.compile(r'(([\d\.-]+)(?::|\s)|(logged in))')
s = "2019.10.20-22.01.33: '10.11.111.25 9999995555884411:TechnoBeceT(69)' logged in"
q = [x[1] or x[2] for x in p.findall(s)]
print(q)
Output
['2019.10.20-22.01.33', '10.11.111.25', '9999995555884411', 'logged in']
Looks like you just need to split by ' ', ':' and 'TechnoBeceT(69)' as an appropriate regex. This existing question is probably what you need: Split string with multiple delimiters in Python

python regex with multiple separators [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 3 years ago.
Improve this question
I am trying to separate all the images from the following string.
how can I get a list of images that start with "comp1/img_" and are either split by a "," or a ";"
/*jsonp*/jsonresp({"img_set":"comp1/img_23434;comp1/img_3243r43r,comp1/img_o43nfjr;comp1/img_wjfno43,comp1/img_nrejfner;comp1/img_jrenckerjv,comp1/img_23434k;comp1/img_rkfnk4n"},"fknreff\",");
so I would end up with a list like...
comp1/img_23434
comp1/img_3243r43r
comp1/img_o43nfjr
comp1/img_wjfno43
comp1/img_nrejfner
comp1/img_jrenckerjv
comp1/img_23434k
comp1/img_rkfnk4n
any help would be appreciated.
thanks
You can do this:
>>> data = '/*jsonp*/jsonresp({"img_set":"comp1/img_23434;comp1/img_3243r43r,comp1/img_o43nfjr;comp1/img_wjfno43,comp1/img_nrejfner;comp1/img_jrenckerjv,comp1/img_23434k;comp1/img_rkfnk4n"},"fknreff\",");'
>>> import re
>>> re.findall(r'comp1/img_[^;,"]+', data)
['comp1/img_23434', 'comp1/img_3243r43r', 'comp1/img_o43nfjr', 'comp1/img_wjfno43', 'comp1/img_nrejfner', 'comp1/img_jrenckerjv', 'comp1/img_23434k', 'comp1/img_rkfnk4n']

Easy way to extract multiple dates from string without spaces? [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 3 years ago.
Improve this question
I am looking for a way to automatically extract dates from a string, but following each other without a delimiter
For example my string is: \n-\n24-04-201923-04-201922-04-201921-04-201920-04-201919-04-201918-04-2019
How can I get this output:
24-04-2019
23-04-2019
22-04-2019
21-04-2019
20-04-2019
19-04-2019
18-04-2019
Any help would be appreciated!
Given that they're all of equal length, you can just clear the \n's then use textwrap:
import textwrap
print(textwrap.wrap(my_string, 10))
You can remove \n's using strip():
my_string = my_string.strip()
You can use this code also.
string='\n-\n24-04-201923-04-201922-04-201921-04-201920-04-201919-04-201918-04-2019'
newStr=string[3:]
for char in range(0,len(newStr),10):
print newStr[char:char+10]
Here's the output
24-04-2019
23-04-2019
22-04-2019
21-04-2019
20-04-2019
19-04-2019
18-04-2019

get everything in a string after a specific symbol in python using regex [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 4 years ago.
Improve this question
Hi I would like to get everything after the '_' using regex
For example: I have --> I want
'aaa_bbb_ccc' --> 'bbb_ccc'
'dd_aaaa_1' --> 'aaaa_1'
'*/_2d*_//' --> '2d*_//'
Is there anyway to do it?
Thanks in advance.
I rather like the split suggestion given by #Maroun in a comment above. Here is an option using re.sub:
x = "aaa_bbb_ccc"
output = re.sub(r'^[^_]+_', '', x)
print(output)
bbb_ccc
The regex does not require much explanation, and it just removes all content up to, and including, the first underscore in the input string.

how to extract a substring from a string in Python with regex? [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 6 years ago.
Improve this question
I have a string
<b>Status : Active<br>Code : C1654<br><br>Shop <a class="top"<b>Shop A</b></a></b>
And I want get Active , C1654 and Shop A.
How to do the same thing in Python?
use python re module
(you didn't explain the pattern you wanted to follow so I can just give an example that will work for the above string):
import re
results = []
reg = '.*?>.*?: (.+?)<br'
my_str = '<b>Status : Active<br>Code : C1654<br><br>Shop <a class="top"<b>Shop A</b></a></b>'
results+=re.findall(reg,my_str)
reg2 = '<a.*?<b>(.*?)</b>'
results += re.findall(reg2,my_str)
print result
>>>['Active', 'C1654', 'Shop A']
I hope I helped

Categories