Finding a combination of characters and digits in a string 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 4 months ago.
Improve this question
I have a list of lists, and I'm attempting to loop through and check to see if the strings in a specific index in each of the inner lists contain a combination of "XY" and then 4 numbers immediately following. The "XY" could be in various locations of the string, so I'm struggling with the syntax beyond just using "XY" in row[5]. How to I add the digits after the "XY" to check? Something that combines the "XY" and isdigit()? Am I stuck using the find function to return an index and then going from there?

You can use Python's regex module re with this pattern that matches XY and then four digits anywhere in the string.
import re
pattern = r'XY\d{4}'
my_list = [['XY0'],['XY1234','AB1234'],['XY1234','ABC123XY5678DEF6789']]
elem_to_check = 1
for row in my_list:
if len(row) > elem_to_check:
for found in re.findall(pattern, row[elem_to_check]):
print(f'{found} found in {row[elem_to_check]}')
Output:
XY5678 found in ABC123XY5678DEF6789

Related

Replace a string in a URL with 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 4 months ago.
Improve this question
I have a text containing a URL that needs to be reworked.
text='dfs:/?url=https://myserver/c12&ofg={"tes":{"id":1812}}'
I need to replace programmatically the id value (in this example 1812, which is unknown before the execution) with a fixed substring (e.g. 189). So the end result must be
'dfs:/?url=https://myserver/c12&ofg={"tes":{"id":189}}'
As I'm programming in Python, I guess that I should use the regular expression (module re) to automatically replace that value between "id": and }} but I couldn't find one that works for this use case.
I assume you are always generating the same URL with that pattern, and the value to 'change' is always in {"id":X}. One way to solve this particular problem is with a positive lookbehind + re.sub replacement.
import re
pattern = re.compile(r"(?<=\"id\":)\d+")
string = "dfs:/?url=https://myserver/c12&ofg={\"tes\":{\"id\":1812}}"
print(pattern.sub("desired_value", string))
Generated output will contain desired_value in place of the 1812. A good explanation of what is happening is done in regex101 but a quick rep of what is happening in the pattern:
Matches any digit one or more times ONLY if behind has "id":, without consuming characters
what about simply splitting the string twice? eg.
my_string = 'dfs:/?url=https://myserver/c12&ofg={"tes":{"id":1812}}'
substring = my_string.split('"id":',1)[1]
substring = substring.split('}}')[0]
print(my_string.replace(substring, "189"))

replace all int's in string with _ [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 10 months ago.
Improve this question
How can I separate data types in a string or list so they can be set to another character, I assume there is something I have missed but everything i have tried so far has now worked for me. so far have tried so split into list and use a for loop to find every int but I can't find a way to differentiate the data types so it can change every int.
You can use regex :
import re
re.sub("\d", "_", "10 4 2")
\d matches any decimal digit character.

How to get a list of characters that lying after a specific key from a 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 4 years ago.
Improve this question
For Example I need each char after '!' in my String ("192.168.!A.!B")
I want a list contains A and B
The String maybe like this "Interface Vlan !A , Name !B"
I would do it with a regular expression:
import re
pattern = r"!(.)"
search_string = "192.168.!A.!B"
results = re.findall(pattern, search_string)
# Returns ['A', 'B']
This also returns an empty list if the ! happens at the end of the string.
regex101 is a good place to learn regular expressions and test them out on strings: https://regex101.com/r/lZClzT/1
Try using split with list comprehension:
print([next(iter(i),None) for i in string.split('!')])
Or in a map-style way:
print(list(map(lambda i: next(iter(i),None),string.split('!'))))
This is the answer by someone delete his answer.
a= re.findall('!(\w)',"192.168.!A.!B")
print(a)

Python regex to remove specific pattern from a list of strings [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 list of strings with filenames. The filenames follow a specific naming format:
string1_YYYYMMDD_HHMMSS_string2
Here YYYYMMDD and HHMMSS are actual date and time values.
I want to delete all characters that appear after 'string1' for each of the entries. I've been trying this with regex but to no vain. Could anyone help me with this?
You don't need a regex, just split on the first underscore:
s = 'string1_YYYYMMDD_HHMMSS_string2'
return s.split('_')[0]
[edit]:
If you can only rely on the last parts ('_YYYYMMDD_HHMMSS_string2') then try indexing like this:
s = 's_t_r_i_n_g_1_YYYYMMDD_HHMMSS_string2'
return '_'.join(s.split('_')[:-3])
Using regex:
import re
s = 'string1_YYYYMMDD_HHMMSS_string2'
newstr = re.sub('_.*', '', s)
print(newstr)
Notes:
_.* matches with a _ and all of its following characters.
re.sub(p, r, s) searches s for p and replaces all matches with r.
Update #1
string1 may contain additional underscores. I'd like to retain all of string1 and only get rid of the trailing pattern.
In this case you can use the following regex:
_\d{8}_\d{6}_.*
Demo: https://regex101.com/r/jS2gL5/1

Removing suffix from list 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 8 years ago.
Improve this question
I have a list of packages and want to remove suffixes from specific combination of characters.
Should I use regex or other mehotds such as replace ? All packages' endings are different because these are versions so I am not sure which to use and how. I think I should clear the remaining characters from -0: in string.
What list is like:
... 'znc-devel-0:1.4-1.el6.i686', 'znc-devel2-0:1.4-1.el6.x86_64' ...
What I want to have:
...'znc-devel', 'znc-devel2'...
What should I do?
partition on "-0" and take the left portion.
seq = ['znc-devel-0:1.4-1.el6.i686', 'znc-devel2-0:1.4-1.el6.x86_64']
print [item.partition("-0")[0] for item in seq]
Result:
['znc-devel', 'znc-devel2']
Through re.sub and list comprehension.
>>> L = ['znc-devel-0:1.4-1.el6.i686', 'znc-devel2-0:1.4-1.el6.x86_64']
>>> import re
>>> [re.sub(r'-0:.*', r'', line) for line in L]
['znc-devel', 'znc-devel2']

Categories

Resources