replace all int's in string with _ [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 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.

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")))

Destroy 2 caracters after a number [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 11 months ago.
Improve this question
I need help, in a long string, I want to know the the 2 characters after a number
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
if in example after 1 number == "gr":
so delete "gr"
If I understand your question correctly, what you need is regex:
import re
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
re.sub("(\d)gr", r"\1", example)
Output
gz15123da1az41sd23f168zgre4r6z
Explnatation
The re.sub function takes three arguments: the first argument is a patter, the second one the replacement, and the last of is the string itself. (\d)gr selects the parts that contain a number followed by gr, then replaces it with the number (\d) itself(removing the gr part).

How to remove all characters from a string after two white spaces in python? [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
The string i needed to format is,
string = "How are you? abcdef"
I need to remove the "abcdef" from the string.
string = string.split(' ')[0]
Edit: Explanation. The line of code above will split the single string into a list of strings wherever there is a double space. It is important to note that whatever is split upon, will be removed. [0] then retrieves the first element in this newly formed list.

Regex in python to catch a string like MMS-2839 [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 7 years ago.
Improve this question
How can I catch some string like these with a regex in python?
M1Sxs-2839
McS-28S9213
Both the first and the second part (divided by the -) can contains letters and numbers (case insensitive).
You may try the below re.match function.
re.match(r"(?i)[A-Z0-9]+-[A-Z0-9]+$", st)
(?i) helps to do case-insensitive match. Since re.match scans the input from start, you don't need to add start of the line anchor ^ explicitly.

Python - how to replace 'p' in a number(4p5) with '.' (4p5->4.5)? [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 program that prints out some data with 'p's in place of decimal points, and also some other information. I was trying to replace the 'p's with '.'s.
Example output by the program:
out_info = 'value is approximately 34p55'
I would like to change it to:
out_info_updated = 'value is approximately 34.55'
I tried using re.search to extract out the number and replace the p with ., but plugging it back becomes a problem.I could not figure out that pattern to use for re.sub that would do the job.
Can anyone please help?
Here you go:
import re
out_info = "value is approximately 34p55"
re.sub(r'(\d+)p(\d+)', r'\1.\2', out_info)
The output is:
'value is approximately 34.55'
That says "Look for one or more digits, followed by a p, followed by one or more digits, then replace all that with the first set of digits, followed by a ., followed by the second set of digits."

Categories

Resources