Check if string follow a strict format via Regex 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 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")))

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.

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)

Handle several adjacent regex matches [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 3 years ago.
Improve this question
Having a regex pattern which can be found several times in a text, how would one chooses and differs between the matches in this case?
choose the first match.
ignore the peripherals.
only one match using index.
Example:
regex_pattern = r"(AB.+?AbB)|(CD.+?CdD)|(EF.+?EfF)"
text = "for finding several CDadjacent CDtagsCdDCdD in a
text this is an ABexampleAbB text"
the first match is CDadjacent CDtagsCdD.
while one might wants to match both:
CDadjacent CDtagsCdDCdD
and. . . . . . . CDtagsCdD
Found the answer faster than I thought.
using '(',')' (round brackets) lets you use parts of the regex as one unit and by adding the '+; (plus sign) you can look for more than one occurrences of a pattern.
for this example this pattern will solve the issue:
r"(AB.+?(AbB)+)|(CD.+?(CdD)+)|(EF.+?(EfF)+)"

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