我正在尝试创建一个正则表达式,以查找最小长度的句子。
我的实际情况是:
到目前为止,我已经尝试过
void createIsoscTriangle(int n) {
for(int i = 1; i <= n; i++)
print(i);
for(int i = n-1; i >= 1; i--)
print(i);
}
如果我的示例文本是:
function Append-ToProfile {
Param(
[Parameter(Mandatory=$true,Position=0)]
[String]$arguments
)
if (-not [String]::IsNullOrEmpty($arguments)){
Add-Content $PROFILE.CurrentUserAllHosts -Value "`n$arguments"
}
else {
code $PROFILE.CurrentUserAllHosts
}
}
我想匹配字符串1和5。
我正在使用python re库。我正在使用regex101进行测试,看来我上面使用的正则表达式在回溯方面做了很多工作,所以我想那些在正则表达式中广为人知的人可能会有些吃惊(我很抱歉)。
答案 0 :(得分:3)
您可以使用以下正则表达式来标识满足所有三个条件的字符串:
^(?!.*\b(\w+)\b.+\b\1\b)(?:.*\b\w+\b){5}.*[.?!]\s*$
设置了不区分大小写的标记。
Python的正则表达式引擎执行以下操作。
^ # match beginning of line
(?! # begin negative lookahead
.+ # match 1+ chars
\b(\w+)\b # match a word in cap grp 1
.+ # match 1+ chars
\b\1\b # match the contents of cap grp 1 with word breaks
) # end negative lookahead
(?: # begin non-cap grp
.+ # match 1+ chars
\b\w+\b # match a word
) # end non-cap grp
{5} # execute non-cap grp 5 times
.* # match 0+ chars
[.?!] # match a punctuation char
\s* # match 0+ whitespaces
$ # match end of line
答案 1 :(得分:0)
第1项和第3项可以通过正则表达式轻松完成,但是
2. words in sequence must be distinct
我看不到如何使用正则表达式模式。请记住,正则表达式是字符串匹配操作;它没有严格的逻辑。对我来说,这个问题听起来不像是正则表达式问题。
我建议将字符" "
中的字符串分开并逐字检查。更快,没有汗水。
修改