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

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

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

问题

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

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

  1. import re
  2. def invalid_syntax(str):
  3. regex_str = r'(?<![^^])\*(?![^$])'
  4. if re.search(regex_str, str):
  5. return True
  6. return False

它应该如下工作:

  1. invalid_syntax('exampleStr') == False
  2. invalid_syntax('*.exampleStr') == False
  3. invalid_syntax('*exampleStr') == True
  4. invalid_syntax('examp*leStr') == True
  5. invalid_syntax('ex*ampleStr') == True
  6. invalid_syntax('exampleStr*') == True
  7. invalid_syntax('*.exampleStr*') == True
  8. invalid_syntax('*.exampleStr.*') == True
  9. 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.

  1. import re
  2. def invalid_syntax(str):
  3. regex_str = &#39;(?&lt;!^)\*(?!$)&#39;
  4. if re.search(regex_str, str):
  5. return True
  6. return False

It should behave as follows:

  1. invalid_syntax(&#39;exampleStr&#39;) == False
  2. invalid_syntax(&#39;*.exampleStr&#39;) == False
  3. invalid_syntax(&#39;*exampleStr&#39;) == True
  4. invalid_syntax(&#39;examp*leStr&#39;) == True
  5. invalid_syntax(&#39;ex*ampleStr&#39;) == True
  6. invalid_syntax(&#39;exampleStr*&#39;) == True
  7. invalid_syntax(&#39;*.exampleStr*&#39;) == True
  8. invalid_syntax(&#39;*.exampleStr.*&#39;) == True
  9. invalid_syntax(&#39;exampleStr.*&#39;) == False

答案1

得分: 2

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

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

以下是一个测试运行:

  1. >>> for s in ['*okay', 'okay*', 'okay', 'not*okay', '*']:
  2. ... print(test(s), repr(s), sep='\t')
  3. True '*okay'
  4. True 'okay*'
  5. True 'okay'
  6. False 'not*okay'
  7. 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:

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

Here is a test run:

  1. &gt;&gt;&gt; for s in [&#39;*okay&#39;, &#39;okay*&#39;, &#39;okay&#39;, &#39;not*okay&#39;, &#39;*&#39;]:
  2. ... print(test(s), repr(s), sep=&#39;\t&#39;)
  3. True &#39;*okay&#39;
  4. True &#39;okay*&#39;
  5. True &#39;okay&#39;
  6. False &#39;not*okay&#39;
  7. 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):

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

或者:

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

或者:

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

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

  1. ^[^*]*$|^\*\.[^*]*$|^[^*]+\.\*$
  • ^[^*]*$ -> 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:

确定