I have to write a regular expression refer to a string.I need to get the part between "$u" and the final "$",also I need match the part before "$u" Now I wrote the regular expression as follow, but it can not work
a='=856 \\$uhttp://sfx-852cuh.hosted$'
#Two backslash may replace by other characters:a='=856 aa$uhttp://sfx-852cuh.hosted$'
result=re.search('=\w{3}\s{2}\S{2}\$u(.*)\$', a)
target_str=result.group(1)
Replace \\S{2}
with \\S{1,2}
if you expect 1 or 2 non-whitespace chars before $u
and do not use a variable with str
name:
import re
a='=856 \\$uhttp://sfx-852cuh.hosted$'
result=re.search(r'=\w{3}\s{2}\S{1,2}\$u(.*)\$', a)
expected_value = ''
if result:
expected_value = result.group(1)
print(expected_value)
See the Python demo
A side note - '\\' in a string is the representation of a single '\\', as it is the start of a special character.
Anyway you can make life easier - if you're guaranteed only a single "$u":
a='=856 \\$uhttp://sfx-852cuh.hosted$'
#Two backslash may replace by other characters:a='=856 aa$uhttp://sfx-852cuh.hosted$'
result=re.search('(.*)\$u(.*)\$', a)
NOT_str=result.group(2)
Group 1 contains the stuff before the $u
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.