I need to format many strings that contain a similar structure:
u'LastName FirstName (Department / Subdepartment)'
My wish is to get the string to look like this:
u'LastName FirstName \(Department / Subdepartment\)'
Meaning I need to add a backslash to the opening bracket and to the closing bracket.
So far I am doing this in Python:
displayName = displayName.replace('(', '\(').replace(')', '\)').
Which seems OK, but I am just wondering:
Is there is a more Pythonic way to do it?
I did not find a proper way Python's String documentation, but maybe I am looking in the wrong place...
You've already found the most Pythonic way, regex provides a not so readable solution:
>>> import re
>>> s = u'LastName FirstName (Department / Subdepartment)'
>>> print re.sub(r'([()])', r'\\\1', s)
LastName FirstName \(Department / Subdepartment\)
you can use re.escape('string').
example:
import re
escaped = re.escape(u'LastName FirstName (Department / Subdepartment)')
Note:
This method will return the string with all non-alphanumerics backslashed which includes punctuation and white-space.
Though that may be useful for you.
Related
Very poor title - feel free to update it if you feel you can help
I'm trying to return a list
[<str1>, <str2>,...,<strX>]
in the following string:
'%%<str1>%%_Anything_Can_Be_Here_%%<str2>%%'
The following code works, but if the number of '%%'s in the line is greater than 2, it takes everything between the first and last set of '%%'.
>>> import re
>>> str = '%%nas_ip_address%%'
>>> re.match('%%(.*)%%', str, re.DOTALL).group(1)
'nas_ip_address'
>>> str = '%%nas_ip_address%%:/vx/%%sfs_storage_pool%%'
>>> re.match('%%(.*)%%', str, re.DOTALL).group(1)
'nas_ip_address%%:/vx/%%sfs_storage_pool'
>>> re.match('%%(.*)%%', str, re.DOTALL).groups()
('nas_ip_address%%:/vx/%%sfs_storage_pool',)
Is there a way to somehow extract ['nas_ip_address', 'sfs_storage_pool'] from the string using regex? I'm looking to parse a very large file however the performance is not an issue as it's not for production
You can use re.findall() if you want to match multiple results in the same string
Try this:
import re
str = '%%nas_ip_address%%:/vx/%%sfs_storage_pool%%'
re.findall('%%(.*?)%%', str, re.DOTALL)
Because * is greedy by default, which means it will consume everything till the end, then backtrack one character at a time until finding the closest %%, close to the end of string then.
Two options to prevent it:
use lazy quantifier *?
even better if no risk of an occuring % in between, add contrast and use a negated character class [^%]*.
Programming in Python3.
I am having difficulty in controlling whether a string meets a specific format.
So, I know that Python does not have a .contain() method like Java but that we can use regex.
My code hence will probably look something like this, where lowpan_headers is a dictionary with a field that is a string that should meet a specific format.
So the code will probably be like this:
import re
lowpan_headers = self.converter.lowpan_string_to_headers(lowpan_string)
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(lowpan_headers[dest_addrS])
However, my issue is in the format and I have not been able to get it right.
The format should be like bbbb00000000000000170d0000306fb6, where the first 4 characters should be bbbb and all the rest, with that exact length, should be hexadecimal values (so from 0-9 and a-f).
So two questions:
(1) any easier way of doing this except through importing re
(2) If not, can you help me out with the regex?
As for the regex you're looking for I believe that
^bbbb[0-9a-f]{28}$
should validate correctly for your requirements.
As for if there is an easier way than using the re module, I would say that there isn't really to achieve the result you're looking for. While using the in keyword in python works in the way you would expect a contains method to work for a string, you are actually wanting to know if a string is in a correct format. As such the best solution, as it is relatively simple, is to use a regular expression, and thus use the re module.
Here is a solution that does not use regex:
lowpan_headers = 'bbbb00000000000000170d0000306fb6'
if lowpan_headers[:4] == 'bbbb' and len(lowpan_headers) == 32:
try:
int(lowpan_headers[4:], 16) # tries interpreting the last 28 characters as hexadecimal
print('Input is valid!')
except ValueError:
print('Invalid Input') # hex test failed!
else:
print('Invalid Input') # either length test or 'bbbb' prefix test failed!
In fact, Python does have an equivalent to the .contains() method. You can use the in operator:
if 'substring' in long_string:
return True
A similar question has already been answered here.
For your case, however, I'd still stick with regex as you're indeed trying to evaluate a certain String format. To ensure that your string only has hexadecimal values, i.e. 0-9 and a-f, the following regex should do it: ^[a-fA-F0-9]+$. The additional "complication" are the four 'b' at the start of your string. I think an easy fix would be to include them as follows: ^(bbbb)?[a-fA-F0-9]+$.
>>> import re
>>> pattern = re.compile('^(bbbb)?[a-fA-F0-9]+$')
>>> test_1 = 'bbbb00000000000000170d0000306fb6'
>>> test_2 = 'bbbb00000000000000170d0000306fx6'
>>> pattern.match(test_1)
<_sre.SRE_Match object; span=(0, 32), match='bbbb00000000000000170d0000306fb6'>
>>> pattern.match(test_2)
>>>
The part that is currently missing is checking for the exact length of the string for which you could either use the string length method or extend the regex -- but I'm sure you can take it from here :-)
As I mentioned in the comment Python does have contains() equivalent.
if "blah" not in somestring:
continue
(source) (PythonDocs)
If you would prefer to use a regex instead to validate your input, you can use this:
^b{4}[0-9a-f]{28}$ - Regex101 Demo with explanation
Given this string:
GET /dsadda HTTP/1.1\r\nUser-Agent: curl/7.26.0\r\nHost: www.youtube.com\r\nAccept: */*\r\n\r\n
How would I obtain everything in a Python regex group between Host: and \r\n?
In this example, I would like re.match.group(1) to return www.youtube.com
You could use this Regex to match
>>> a = 'GET /dsadda HTTP/1.1\r\nUser-Agent: curl/7.26.0\r\nHost: www.youtube.com\r\nAccept: */*\r\n\r\n'
>>> import re
>>> re.search(r"Host: (.+)\r\n",a).group(1)
'www.youtube.com'
Small Note - It is better to use re.MULTILINE flag as the input string contains \n, though it is not required in this particular case.
Additionally, As Antti Haapala mentions, using the ^ to match the start of the string is also a better option, as there may be Header fields with the name Host. Thus the final regex would be something like re.search(r"^Host: (.+)\r\n",a,re.M).group(1).
using positive look behind and positive look ahead
>>> import re
>>> a = 'GET /dsadda HTTP/1.1\r\nUser-Agent: curl/7.26.0\r\nHost: www.youtube.com\r\nAccept: */*\r\n\r\n'
>>> re.search(r"(?<=Host: )(\S+)(?=\r\n)", a).group(1)
'www.youtube.com'
Please help with my regex problem
Here is my string
source="http://www.amazon.com/ref=s9_hps_bw_g200_t2?pf_rd_m=ATVPDKIKX0DER&pf_rd_i=3421"
source_resource="pf_rd_m=ATVPDKIKX0DER"
The source_resource is in the source may end with & or with .[for example].
So far,
regex = re.compile("pf_rd_m=ATVPDKIKX0DER+[&.]")
regex.findall(source)
[u'pf_rd_m=ATVPDKIKX0DER&']
I have used the text here. Rather using text, how can i use source_resource variable with & or . to find this out.
If the goal is to extract the pf_rd_m value (which it apparently is as you are using regex.findall), than I'm not sure regex are the easiest solution here:
>>> import urlparse
>>> qs = urlparse.urlparse(source).query
>>> urlparse.parse_qs(qs)
{'pf_rd_m': ['ATVPDKIKX0DER'], 'pf_rd_i': ['3421']}
>>> urlparse.parse_qs(qs)['pf_rd_m']
['ATVPDKIKX0DER']
You also have to escape the .
pattern=re.compile(source_resource + '[&\.]')
You can just build the string for the regular expression like a normal string, utilizing all string-formatting options available in Python:
import re
source_and="http://rads.stackoverflow.com/amzn/click/B0030DI8NA/pf_rd_m=ATVPDKIKX0DER&"
source_dot="http://rads.stackoverflow.com/amzn/click/B0030DI8NA/pf_rd_m=ATVPDKIKX0DER."
source_resource="pf_rd_m=ATVPDKIKX0DER"
regex_string = source_resource + "[&\.]"
regex = re.compile(regex_string)
print regex.findall(source_and)
print regex.findall(source_dot)
>>> ['pf_rd_m=ATVPDKIKX0DER&']
['pf_rd_m=ATVPDKIKX0DER.']
I hope this is what you mean.
Just take note that I modified your regular expression: the . is a special symbol and needs to be escaped, as is the + (I just assumed the string will only occur once, which makes the use of + unnecessary).
I am trying to use re.findall with look-behind and look-forward to extract data. The regular expression works fine when I am not using a raw_input variable, but I need users to be able to input a variety of different search terms.
Here is the current code:
me = re.findall(r"(?<='(.+)'+variable+'(.+)')(.*?)(?='(.+)+variable+(.+)')", raw)
As you can see, I am trying to pull out strings between one search term.
However, each time I use this type of formatting, I get a fixed width error. Is there anyway around this?
I have also tried the following formats with no success.
variable = raw_input('Term? ')
'.*' + variable + '.*'
and
'.*%s.*' % (variable, )
and
'.*{0}.*'.format(variable)
and
'.*{variable}.*'.format(variable=variable)
I'm not sure if this is what you mean, but it may get you started. As far as I understand your question, you don't need lookaheads or lookbehinds. This is for Python 2.x (won't work with Python 3):
>>> import re
>>> string_to_search = 'fish, hook, swallowed, reeled, boat, fish'
>>> entered_by_user = 'fish'
>>> search_regex = r"{0}(.+){0}".format(entered_by_user)
>>> match = re.search(search_regex, string_to_search)
>>> if match:
... print "result:", match.group(1).strip(' ,')
...
result: hook, swallowed, reeled, boat
If you really want the last 'fish' in the result as in your comment above, then just remove the second {0} from the format() string.
This solution should work:
me = re.findall(rf"(?<='(.+)'+{variable}+'(.+)')(.*?)(?='(.+)+{variable}+(.+)')", raw)
You also can add many different variables as you wish.
Add rf for the regular expression and the desired variables in between {}
import re
text = "regex is the best"
var1 = "is the"
var2 = "best"
yes = re.findall(rf"regex {var1} {var2}", text)
print(yes)
['regex is the best']
The way lookbehind is usually implemented (including its Python implementation) has an inherent limitation that you are unfortunately running into: lookbehinds cannot be variable-length. The "Important Notes About Lookbehind" section here explains why. I think you should be able to do the regex without a lookbehind, though.