Python字符串的正则表达式,不以特定字符开头或结尾。

huangapple go评论70阅读模式
英文:

Regex for a python string that does not start or end with specific characters

问题

我需要处理输入字符串,不能在中间包含 *,但如果它以 * 开始或结束,那么它需要以 *. 开始或以 .* 结束(不能同时出现,最多允许一个 *)。

我已经完成了检查是否在中间有 * 的第一部分,但如何添加一个条件,如果它只以 * 开始或结束,而没有 .,也是无效的。

import re

def invalid_syntax(str):
    regex_str = r'(?<![^^])\*(?![^$])'
    if re.search(regex_str, str):
        return True
    return False

它应该如下工作:

invalid_syntax('exampleStr') == False
invalid_syntax('*.exampleStr') == False
invalid_syntax('*exampleStr') == True
invalid_syntax('examp*leStr') == True
invalid_syntax('ex*ampleStr') == True
invalid_syntax('exampleStr*') == True
invalid_syntax('*.exampleStr*') == True
invalid_syntax('*.exampleStr.*') == True
invalid_syntax('exampleStr.*') == False
英文:

I need to process input strings that cannot have * in the middle, but if it does start or end with *, it needs to start with *. or end with .* (cannot have both, max of one * allowed)

I have the first part of checking if it has * in the middle, but how do I add on that if it starts or ends with just * without the . it is also invalid.

import re

def invalid_syntax(str):
    regex_str = &#39;(?&lt;!^)\*(?!$)&#39;
    if re.search(regex_str, str):
        return True
    return False

It should behave as follows:

invalid_syntax(&#39;exampleStr&#39;) == False
invalid_syntax(&#39;*.exampleStr&#39;) == False
invalid_syntax(&#39;*exampleStr&#39;) == True
invalid_syntax(&#39;examp*leStr&#39;) == True
invalid_syntax(&#39;ex*ampleStr&#39;) == True
invalid_syntax(&#39;exampleStr*&#39;) == True
invalid_syntax(&#39;*.exampleStr*&#39;) == True
invalid_syntax(&#39;*.exampleStr.*&#39;) == True
invalid_syntax(&#39;exampleStr.*&#39;) == False

答案1

得分: 2

如果正则表达式证明很困难,考虑使用标准字符串操作代替。这可能会导致更符合问题描述并更容易理解/维护的代码:

def test(s):
    """ 我需要处理的输入字符串不能包含*,但如果它以*开始或结束,则需要以*开头或以.*结尾(不能同时具有两者,最多允许一个*) """
    c = s.count('*')
    return c == 0 or c == 1 and (s.startswith('*') or s.endswith('*'))

以下是一个测试运行:

>>> for s in ['*okay', 'okay*', 'okay', 'not*okay', '*']:
...     print(test(s), repr(s), sep='\t')
True	'*okay'
True	'okay*'
True	'okay'
False	'not*okay'
True    '*'

请注意,问题说明对于是否允许单个*不是100%清楚。可能应该拒绝这种情况,因为它既以开头又以结尾。要添加这个要求,只需添加 and len(s) != 1

英文:

If regexes prove to be a struggle, consider using standard string operations instead. That may result in code that more closely corresponds to the problem statement and be easier to understand/maintain:

def test(s):
    &quot;&quot;&quot; I need to process input strings that cannot have * in the
        middle, but if it does start or end with *, it needs to start
        with *. or end with .* (cannot have both, max of one * allowed)
    &quot;&quot;&quot;
    c = s.count(&#39;*&#39;)
    return c == 0 or c == 1 and (s.startswith(&#39;*&#39;) or s.endswith(&#39;*&#39;))

Here is a test run:

&gt;&gt;&gt; for s in [&#39;*okay&#39;, &#39;okay*&#39;, &#39;okay&#39;, &#39;not*okay&#39;, &#39;*&#39;]:
...     print(test(s), repr(s), sep=&#39;\t&#39;)
True	&#39;*okay&#39;
True	&#39;okay*&#39;
True	&#39;okay&#39;
False	&#39;not*okay&#39;
True    &#39;*&#39;

Note, the problem specification is not 100% clear about whether a single * is allowed. Possibly this should be rejected because it both starts with and ends with a star. To add this requirement, just add and len(s) != 1.

答案2

得分: 1

如果您想要一个正则表达式,可以尝试使用 (regex101):

^[^*]*$|^\*\.[^*]*$|^[^*]+\.\*$
  • ^[^*]*$ - 匹配如果字符串不包含 *

或者:

  • ^\*\.[^*]*$ - 匹配如果字符串以 *. 开始并且不包含额外的 *

或者:

  • ^[^*]+\.\*$ - 匹配如果字符串以 .* 结尾并且不包含额外的 *
英文:

If you want one regex you can try (regex101):

^[^*]*$|^\*\.[^*]*$|^[^*]+\.\*$
  • ^[^*]*$ -> match if string doesn't contain *

or:

  • ^\*\.[^*]*$ -> match if string begins with *. and doesn't contain additional *

or:

  • ^[^*]+\.\*$ -> match if string ends with .* and doesn't contain additional *

huangapple
  • 本文由 发表于 2023年6月1日 06:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定