如何在单词之间只允许 3 种类型的字符? [英] How to allow only 3 types of characters between words?
问题描述
我有一组允许的字符:
撇号`
空白区域
empty space
破折号 -
我正在努力构建一个正则表达式:
I'm struggling with building a regex which:
一个.每个单词之间只允许出现一次允许的字符(允许任意数量的单词)
a. allows only one occurrence of the allowed characters between each word (any number of words are allowed)
E.g.
text-text --> VALID
text text --> VALID
text`text --> VALID
B.允许允许字符的组合,但不能一个接一个
b. allows combinations of allowed characters but not one after the other
E.g.
text-text`text --> VALID
text text-text --> VALID
text`text text --> VALID
text``text text --> INVALID
text`text text --> INVALID
text`text -text --> INVALID
c.不允许以空格 撇号` 或破折号
-
开头,也不允许以撇号 ` 或破折号 -
结尾,但可以以[空格]结束
c. doesn't allow to start with empty space apostrophe ` or dash
-
and doesn't allow to end with apostrophe ` or dash -
but can end with [emptyspace]
E.g.
text[emptyspace] --> VALID
[emptyspace]text --> INVALID
`text --> INVALID
text` --> INVALID
-text --> INVALID
text- --> INVALID
d.完全不允许特殊字符
d. Special characters are not allowed at all
e.完全不允许数字
这是我到目前为止所拥有的:https://regex101.com/r/9i3vq2/5
This is what I have so far: https://regex101.com/r/9i3vq2/5
推荐答案
您可以使用
^[a-zA-Z]+(?:[ `-][a-zA-Z]+)* ?$
查看正则表达式演示
详情
^
- 字符串的开始[a-zA-Z]+
- 1+ 个 ASCII 字母(?:[ `-][a-zA-Z]+)*
- 0 次或多次重复[ `-]
- 空格、反引号或-
[a-zA-Z]+
- 1+ 个 ASCII 字母
^
- start of string[a-zA-Z]+
- 1+ ASCII letters(?:[ `-][a-zA-Z]+)*
- 0 or more repetitions of[ `-]
- a space, backtick or-
[a-zA-Z]+
- 1+ ASCII letters
这篇关于如何在单词之间只允许 3 种类型的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!