英文:
Matching a string with a wildcard and at least two preceeding characters
问题
我正在尝试创建一个正则表达式,它接受:
- 字母(大写或小写)
- 句点、逗号、连字符、撇号和空格
- 如果使用通配符,必须至少在前面有两个前述字符
- 字符串长度必须至少为一个字符(如果不使用通配符)并且至少为30个字符。
我已经构建了这个正则表达式,但它目前不符合预期的行为:
/^(?:[A-Za-z.,'\\-\\s]|[^%]%(?:.{2,}|)) {1,30}$/
你有没有建议如何优化它以满足所需的行为?提前谢谢。
编辑:
以下是有效匹配:
asdSRVGD
as%
assvd-,'
asdbSERF,%
英文:
I'm trying to create a regex that accepts:
- Alphabetic (upper or lowercase)
- full stops, commas, hyphens, apostrophes and spaces
- If using a wildcard, it must be preceded by at least two of the aformentioned characters
- The string must be a minimum of one character (if not using wildcard) and 30 characters in length.
I've constructed this regex but it's not currently working as desired:
/^(?:[A-Za-z.,'\\-\\s]|[^%]%(?:.{2,}|)) {1,30}$/
Do you have any suggestions on how to refine this to meet desired behaviour? Thanks in advance.
EDIT:
These are valid matches:
asdSRVGD
as%
assvd-,'
asdbSERF,%
答案1
得分: 1
我不确定我是否完全理解了要求,但似乎您想要类似这样的东西:
(?i) # 忽略大小写匹配
^ # 字符串开始
(?=.{1,30}$) # 字符串长度大于等于1且小于等于30
(?: # 包含以下之一
[a-z.,'\-\h]{2}% # 两个允许字符,后面跟一个百分号
| # 或者
[a-z.,'\-\h] # 允许的字符中的一个
)+ # 一次或多次
$ # 字符串结尾
这是PCRE语法,但可以轻松转换为其他正则表达式的语法,包括不支持预查的语法(通过单独检查长度)。
在 regex101.com 上试试吧。
英文:
I'm not entirely sure if I understand the requirements correctly, but it seems that you want something like this:
(?i) # Match case-insensitively
^ # at the beginning
(?=.{1,30}$) # a string whose length is >= 1 and <= 30
(?: # which contains either
[a-z.,'\-\h]{2}% # two of the permitted characters followed by a percent sign
| # or
[a-z.,'\-\h] # a single one of those
)+ # 1 or more times
$ # right before the end.
This is in PCRE, but it should be easy to transpile it into other flavors of regex, including those which doesn't support lookarounds (by checking the length separately).
Try it on regex101.com.
答案2
得分: 1
以下是翻译好的内容:
这些示例似乎具有一个可选的通配符。
如果支持先行断言(注意,\s
也可以匹配换行符):
^(?=.{1,30}$)[a-zA-Z.,'\s-]*(?:[a-zA-Z.,'\s-]{2}%[a-z.,'\s-]*)?$
解释
^(?=.{1,30}$)
从字符串的开头到结尾断言匹配 1-30 个字符[a-zA-Z.,'\s-]*
可选地重复匹配列出的任何字符(?:
非捕获组[a-zA-Z.,'\s-]{2}%
匹配列出的字符中的任何字符 2 次,然后%
[a-z.,'\s-]*
可选地重复匹配列出的任何字符
)?
结束组并使其成为可选的$
字符串的结尾
正则表达式演示(使用空格代替 \s
)
英文:
The examples seem to have a single optional wildcard.
If a lookahead is supported (note that \s
can also match a newline):
^(?=.{1,30}$)[a-zA-Z.,'\s-]*(?:[a-zA-Z.,'\s-]{2}%[a-z.,'\s-]*)?$
Explanation
^(?=.{1,30}$)
Assert 1-30 char from the start till end of string[a-zA-Z.,'\s-]*
Optionally repeat matching any of the listed chars(?:
Non capture group[a-zA-Z.,'\s-]{2}%
Match 2 times any of the listed and then `5~[a-z.,'\s-]*
Optionally repeat matching any of the listed chars
)?
Close the group and make it optional$
End of string
Regex demo (using a space instead of \s
)
答案3
得分: 0
这与您的所有示例匹配:
^([A-Za-z.,' -]{1,30}|[A-Za-z.,' -]{2}[A-Za-z.,' %-]{1,28})$
它匹配最多30个非%
字符或两个非%
字符后跟最多28个任意字符,其中字符来自集合A-Za-z.,' %-
。
英文:
This matches all your examples:
^([A-Za-z.,' -]{1,30}|[A-Za-z.,' -]{2}[A-Za-z.,' %-]{1,28})$
It matches either up to 30 non-%
characters or two non-%
characters followed by up to 28 arbitrary characters, where characters are from the set A-Za-z.,' %-
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论