英文:
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 = '(?<!^)\*(?!$)'
if re.search(regex_str, str):
return True
return False
It should behave as follows:
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
答案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):
""" 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)
"""
c = s.count('*')
return c == 0 or c == 1 and (s.startswith('*') or s.endswith('*'))
Here is a test run:
>>> 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 '*'
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*
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论