简体   繁体   English

找到字符串中子字符串的位置

[英]find position of a substring in a string

i am having a python string of format 我有一个格式的python字符串

mystr = "hi.this(is?my*string+"

here i need to get the position of 'is' that is surrounded by special characters or non-alphabetic characters (ie second 'is' in this example). 在这里,我需要获得被特殊字符或非字母字符包围的'is'的位置(即本例中的第二个'是')。 however, using 但是,使用

mystr.find('is')

will return the position if 'is' that is associated with 'this' which is not desired. 如果'is'与'this'相关联,则返回该位置,这是不希望的。 how can i find the position of a substring that is surrounded by non-alphabetic characters in a string? 如何找到字符串中非字母字符包围的子字符串的位置? using python 2.7 使用python 2.7

Here the best option is to use a regular expression. 这里最好的选择是使用正则表达式。 Python has the re module for working with regular expressions. Python具有用于处理正则表达式re模块

We use a simple search to find the position of the "is" : 我们使用简单的搜索来找到"is"的位置:

>>> match = re.search(r"[^a-zA-Z](is)[^a-zA-Z]", mystr)

This returns the first match as a match object. 这会将第一个匹配作为匹配对象返回。 We then simply use MatchObject.start() to get the starting position: 然后我们简单地使用MatchObject.start()来获取起始位置:

>>> match.start(1)
8

Edit: A good point made, we make "is" a group and match that group to ensure we get the correct position. 编辑:一个好点,我们使"is"一个组并匹配该组以确保我们获得正确的位置。

As pointed out in the comments, this makes a few presumptions. 正如评论中所指出的,这做了一些假设。 One is that surrounded means that "is" cannot be at the beginning or end of the string, if that is the case, a different regular expression is needed, as this only matches surrounded strings. 一个是包围意味着"is"不能在字符串的开头或结尾,如果是这种情况,则需要不同的正则表达式,因为这只匹配包围的字符串。

Another is that this counts numbers as the special characters - you stated non-alphabetic , which I take to mean numbers included. 另一个是,这将数字视为特殊字符 - 你说的是非字母的 ,我用它来表示包含的数字。 If you don't want numbers to count, then using r"\\b(is)\\b" is the correct solution. 如果您不想计算数字,那么使用r"\\b(is)\\b"是正确的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM