For example:
My string is: 123456789 nn nn oo nn nn mlm nn203
.
My target is: nn
.
例如:我的字符串是:123456789 nn nn oo nn mlm nn203。我的目标是:神经网络。
Then, I match string from the end to the beginning and return the first match result and its postion.
In this examlpe, the result is nn
start in [-5] end in [-3].
I wrote the simple funcitonto do this process, but how to use regular expressions to do this job?
然后,从结束到开始匹配字符串,并返回第一个匹配结果及其位置。在本例中,结果是nn从[-5]端开始[-3]。我编写了这个简单的函数来完成这个过程,但是如何使用正则表达式来完成这个任务呢?
3 个解决方案
#1
10
For the string itself, just do a findall and use the last one:
对于字符串本身,只需执行findall并使用最后一个:
import re
st='123456 nn1 nn2 nn3 nn4 mlm nn5 mlm'
print re.findall(r'(nn\d+)',st)[-1]
Prints nn5
打印nn5
You can also do the same thing using finditer
which makes it easier finding the relevant indexes:
你也可以使用finditer来做同样的事情,这样更容易找到相关的索引:
print [(m.group(),m.start(),m.end()) for m in re.finditer(r'(nn\d+)',st)][-1]
Prints ('nn5', 27, 30)
打印(nn5,27日,30)
If you have a lot of matches and you only want the last, sometimes it makes sense to simply reverse the string and pattern:
如果你有很多匹配项,而你只想要最后一个,有时候简单地反转字符串和模式是有意义的:
m=re.search(r'(\d+nn)',st[::-1])
offset=m.start(1)
print st[-m.start(1)-len(m.group(1)):-m.start(1)]
Prints nn5
打印nn5
#2
3
First, if you're not looking for a regular expression, string.rfind
is a lot easier to get right.
首先,如果您不需要正则表达式string。rfind更容易做对。
You can use a regular expression by using a negative lookahead, see the documentation of re:
您可以使用一个正则表达式使用一个否定的前视,请参阅re的文档:
import re
s = "123456789 nn nn oo nn nn mlm nn203"
match = re.search("(nn)(?!.*nn.*)", s)
# for your negative numbers:
print (match.start()-len(s), match.end()-len(s))
# (-5, -3)
#3
2
Idea:
的想法:
- find reversed regexp (in your case irrelevant) in reversed string
- 在反向字符串中查找反向regexp(在您的例子中是不相关的)
- resulting indexes convert to negative numbers + switch start<->end
- 结果索引转换为负数+开关开始<->结束
Example:
例子:
>>> import re
>>> s = "123456789 nn nn oo nn nn mlm nn203"
>>> m = re.search("(nn)", s[::-1])
>>> -m.end(), -m.start()
(-5, -3)
#1
10
For the string itself, just do a findall and use the last one:
对于字符串本身,只需执行findall并使用最后一个:
import re
st='123456 nn1 nn2 nn3 nn4 mlm nn5 mlm'
print re.findall(r'(nn\d+)',st)[-1]
Prints nn5
打印nn5
You can also do the same thing using finditer
which makes it easier finding the relevant indexes:
你也可以使用finditer来做同样的事情,这样更容易找到相关的索引:
print [(m.group(),m.start(),m.end()) for m in re.finditer(r'(nn\d+)',st)][-1]
Prints ('nn5', 27, 30)
打印(nn5,27日,30)
If you have a lot of matches and you only want the last, sometimes it makes sense to simply reverse the string and pattern:
如果你有很多匹配项,而你只想要最后一个,有时候简单地反转字符串和模式是有意义的:
m=re.search(r'(\d+nn)',st[::-1])
offset=m.start(1)
print st[-m.start(1)-len(m.group(1)):-m.start(1)]
Prints nn5
打印nn5
#2
3
First, if you're not looking for a regular expression, string.rfind
is a lot easier to get right.
首先,如果您不需要正则表达式string。rfind更容易做对。
You can use a regular expression by using a negative lookahead, see the documentation of re:
您可以使用一个正则表达式使用一个否定的前视,请参阅re的文档:
import re
s = "123456789 nn nn oo nn nn mlm nn203"
match = re.search("(nn)(?!.*nn.*)", s)
# for your negative numbers:
print (match.start()-len(s), match.end()-len(s))
# (-5, -3)
#3
2
Idea:
的想法:
- find reversed regexp (in your case irrelevant) in reversed string
- 在反向字符串中查找反向regexp(在您的例子中是不相关的)
- resulting indexes convert to negative numbers + switch start<->end
- 结果索引转换为负数+开关开始<->结束
Example:
例子:
>>> import re
>>> s = "123456789 nn nn oo nn nn mlm nn203"
>>> m = re.search("(nn)", s[::-1])
>>> -m.end(), -m.start()
(-5, -3)