Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to use regex to see if the given string is an IPv4 address. I want to return a boolean value True/False depending on the string. This is my code:
import re
def isIPv4Address(inputString):
pattern = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s')
return pattern.match(inputString)
The value is null. At this point, I can tell that the function does not return a boolean value. However, all questions I see about regex and IP addresses is about writing the pattern instead of a full implementation. I know that the actual implementation shouldn't be any longer than this because it just takes the input and compares it against the regex.
match returns the match (a re.Match object) or None if the expression doesn't match. If you want to return a boolean whether the regex matches, you probably want to use pattern.match(inputString) is not None
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I have converted integer value to string although it is not returning in quotes ad strings does
[cmd ][1]
print(str(31))
31 #I think it should give result as "31" because now it is string
When string is printed, quotes are usually not added. If you wanna see quote then use the repr() function. For example,
print(repr("31"))
Because while returning it prints the value, the double quotes are not printed. Though it will be treated as a string, you can verify it by using concatenation through + or string multiplication as str(32)*3 this will give you 323232
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a string I am trying to create a regex for in order to extract everything inside the brackets. An example of such a string is as follows
[-At(A),+CarAt(B),-CarAt(A),-InCar]
The current regex I'm using is re.search(r'\[.*?\]', string), but this only returns -At(A),-InCar instead of -At(A),+CarAt(B),-CarAt(A),-InCar
I am not sure why it's matching one set of parentheses in -At(A); I thought the regex I had would work because it would match everything between the brackets.
How can I get everything inside the brackets of my original string?
I think the problem is with the question mark. Because question marks, when they come after a quantifiers make them 'lazy'.
So try to use:
r'\[.*\]'
You didn't say you wanted the contained members, but I suspect it to be the eventual case
To do so, I've found it better to slice or .strip() brackets off and then .split() this sort of string to get its members before doing further validation
>>> s = "[-At(A),+CarAt(B),-CarAt(A),-InCar]"
>>> s = s.strip('[]')
>>> s
'-At(A),+CarAt(B),-CarAt(A),-InCar'
>>> values = s.split(',')
>>> values
['-At(A)', '+CarAt(B)', '-CarAt(A)', '-InCar']
Using a regex to validate the individual results of this is often
easier to write and explain
is better at highlighting mismatches than re.findall(), which will silently omit mismatches
can be much more computationally efficient (though it may not be for your case) than trying to do the operation in a single step (ex1 ex2)
>>> import re
>>> RE_wanted = re.compile(r"[+-](At|Car|In){1,2}(\([A-Z]\))?")
>>> all((RE_wanted.match(a) for a in values))
True
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to replace the substring 'gta' with substring 'cat'. But the condition is that 'gta' immediately has to be followed by substring 'dog'.
Example: 'gtagtadogcat' would become 'gtacatdogcat'
The part I'm struggling with is trying to write the program to find 'gta' and validate that 'dog' is behind it and if true, change 'gta' to 'cat'.
>>> 'gtagtadogcat'.replace('gta'+'dog', 'cat'+'dog')
'gtacatdogcat'
old_string = 'gtagtadogcat'
print(old_string.replace('gtacat','dogcat'))
output: gtagtadogcat
You could use regex:
re.sub('gta(dog)', r'cat\1', 'gtagtadogcat')
Output:
'gtacatdogcat'
*Edit: You would not need a forloop if you put in the whole string. Here is an example:
re.sub('gta(dog)', r'cat\1', 'gtagtadogcat_moretextgta_lastgtadog')
Output:
'gtacatdogcat_moretextgta_lastcatdog'
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here is what I currently have, which is not working:
if "Forwarder" not in shp_name or "T_" not in shp_name or "Grad" not in shp_name:
I've also tried:
if ("Forwarder", "T_", "Grad") not in shp_name:
Samples of the input would be "DS_Forwarder_1" or "DS_Harvester_1". The script proceeds directly to else as it's unable to identify any of the above substrings in the primary string.
Try using the any built in.
if any(s in shp_name for s in ("Forwarder", "T_", "Grad")):
...
This will be true if any of the given strings are present in shp_name. You can use if not any(... if you want False when one of the strings is present.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am having a hard time understanding str.partition() function in python. I have read the definition of the function and searched online without finding an explanation that makes sense to me.
I have some code that uses it pretty heavily and have been trying to understand it. I could post the code if it would help but it is a pretty precise code segment that would probably complicate things.
Need in-depth, probably low-level, explanation of str.partition() function in python.
The docs are pretty clear ...
Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.
So ...
>>> 'test'.partition('s')
('te', 's', 't')
>>> 'test'.partition('a')
('test', '', '')
You either get the front, splitter character, and tail, or you get the full string and two blank strings (depending on whether or not the partition character is present).