简体   繁体   English

如何在 python 中为以下模式编写正则表达式?

[英]How do i write a regular expression for the following pattern in python?

How do i look for the following pattern using regular expression in python?如何在 python 中使用正则表达式查找以下模式? for the two cases对于这两种情况

Am looking for str2 after the "=" sign在“=”符号后寻找str2

  • Case 1: str1=str2案例一: str1=str2
  • Case 2: str1 = str2案例 2: str1 = str2

please note there can be a space or none between the either side of the "=" sign请注意,“=”符号的两侧之间可以有空格或没有空格

Mine is like this, but only works for one of the cases!我的就是这样,但只适用于其中一种情况!

m=re.search('(?<=str\s\=\s)\w+','str = str2')

returns str2返回 str2

Help!帮助!

Gath加思

if you indeed have only such simple strings to parse you don't need regular expression.如果您确实只有这么简单的字符串要解析,则不需要正则表达式。 you can just partition on = and strip (or even lstrip) last element of a resulting tuple:您可以在=上进行分区并剥离(甚至 lstrip)结果元组的最后一个元素:

>>> case = 'str = str2'
>>> case.partition('=')[2].lstrip()
'str2'

it'll be much faster than regexps.它会比正则表达式快得多。 and just to show how fast i've made a simple test:只是为了展示我做了一个简单的测试有多快:

>>> timeit.timeit("'str1 = str2 '.partition('=')[2].strip()")
0.49051564213846177
>>> timeit.timeit("'str1 = str2 '.split('=')[1].strip()")
0.97673281637025866
>>> timeit.timeit('import re')
0.65663786250422618
re.search(r'=\s*(.*)', 'str = str2').group(1)

or if you just want a single word:或者如果你只想要一个词:

re.search(r'=\s*(\w+)', 'str = str2').group(1)

Extended to specific initial string:扩展到特定的初始字符串:

re.search(r'\bstr\s*=\s*(\w+)', 'str=str2').group(1)

\b = word boundary, so won't match "somestr=foo" \b = 单词边界,所以不会匹配"somestr=foo"

It would be quicker to go trough all options once, instead of searching for single options one at the time:通过所有选项一次 go 会更快,而不是一次搜索单个选项:

option_str = "a=b, c=d, g=h"
options = dict(re.findall(r'(\w+)\s*=\s*(\w+)', option_str))
options['c']  # -> 'd'

If your data is fixed then you can do this without using regex.如果您的数据是固定的,那么您可以在不使用正则表达式的情况下执行此操作。 Just split it on '='.只需将其拆分为'='。 For example:例如:

>>> case1 = "str1=str2"
>>> case2 = "str1 = str2"

>>> str2 = case1.split('=')[1].strip()
>>> str2 = case2.split('=')[1].strip()

This YOURCASE.split('=')[1].strip() statement will work for any cases.这个YOURCASE.split('=')[1].strip()语句适用于任何情况。

Simply use split function只需使用拆分 function

I think a regex is overkill if you only want to deal with the above two cases.如果您只想处理上述两种情况,我认为正则表达式是矫枉过正的。 Here's what I'd do-这就是我要做的-

>>> case1 = "str1=str2"
>>> case2 = "str1 = str2"
>>> case2.split()
['str1', '=', 'str2']
>>> ''.join(case2.split())
'str1=str2'
>>> case1[5:]
'str2'
>>> ''.join(case2.split())[5:]
'str2'
>>> 

Assumption假设

I assume you are looking for the specific token 'str1'.我假设您正在寻找特定的令牌“str1”。 I also assume that str1 can be assigned different values.我还假设 str1 可以分配不同的值。 Something like what you'd have in a configuration file => propertyName = value.类似于您在配置文件中的内容 => propertyName = value。

This is just my opinion.这只是我的看法。

I knew that other ways were possible.我知道其他方式是可能的。 SilentGhost gives a nice (better!) alternative. SilentGhost 提供了一个不错的(更好的!)替代方案。

Hope this helps.希望这可以帮助。

Expanding on @batbrat's answer, and the other suggestions, you can use re.split() to separate the input string.扩展@batbrat 的答案和其他建议,您可以使用re.split()来分隔输入字符串。 The pattern can use \s (whitespace) or an explicit space.该模式可以使用\s (空格)或显式空格。

>>> import re
>>> c1="str1=str2"
>>> c2="str1 = str2"
>>> re.split(' ?= ?',c1)
['str1', 'str2']
>>> re.split(' ?= ?',c2)
['str1', 'str2']
>>> re.split(r'\s?=\s?',c1)
['str1', 'str2']
>>> re.split(r'\s?=\s?',c2)
['str1', 'str2']
>>> 

Two cases:两种情况:

  • (case 1) if there is a single space before the '=', then there must also be a single space after the '=' (情况1)如果'='之前有一个空格,那么'='之后也必须有一个空格

    m=re.search(r'(?<=\S)(?:\s=\s|=)(\w+)','str = str2') print m.group(1)
  • (case 2) otherwise, (情况 2) 否则,

     m=re.search(r'(?<=\S)\s?=\s?(\w+)','str = str2') print m.group(1)

In the first case, you could also use the "(?P=…" construct for the second space or lack of it, but it still wouldn't work for a positive lookbehind assertion , since it wouldn't be a constant length subexpression.在第一种情况下,您也可以对第二个空格使用 "(?P=… .

Related idea: I find using graphical regular expression tool helpful when trying to figure out correct pattern: http://kodos.sf.net .相关想法:我发现使用图形正则表达式工具有助于找出正确的模式: http://kodos.sf.net

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

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