I am unable to give a proper title for this issue I am having, but I am trying to figure out how to write a regex that will be able to match a couple of possible options from the list below.
我无法为我正在处理的这个问题给出一个合适的标题,但是我正在尝试如何编写一个regex,它将能够匹配下面列表中的几个可能的选项。
Describe what rules should regex consider as valid?
请描述regex应该考虑哪些规则是有效的?
- Must start with
dfr
- 必须从dfr
- Must contain one dash
-
after keyword that it begins with - 必须包含一个破折号-后关键字,它开始
- Can contain double dash
--
if it continues with one of those keywordsbase
,root
,x
,y
- 可以包含双破折号——如果它继续使用其中一个关键字,根,x, y ?
- If contains double dash + keyword e.g.
dfr--base
must contain another dash after it e.g.dfr--base-
- 如果包含双破折号+关键字例如dfr- base必须在后面包含另一个破折号例如dfr- base-
- Afterwards it can contain only letters, it should validate lowercase and camelCase only.
- 之后它只能包含字母,它应该只验证小写和camelCase。
- Should be chained with single dash
-
and contain letters afterwards - 应该用一个破折号连接——然后包含字母
To clarify with some examples, those should be valid:
为了澄清一些例子,这些例子应该是有效的:
dfr--base-color-dark
dfr--root-size
dfr--x-spacing
dfr-spacing
dfr-fontWeight
dfr-borderRadius-extraSmall
dfr-grid-columns
I think that I'm very bad with regex, but of course I spent some time trying to figure this out... Playing around with RegExr.com
我认为我对regex很不满意,但是我当然花了一些时间试图弄明白……和RegExr.com
^dfr-((-?(base|root|x|y)+([a-zA-Z-])+)$|([a-zA-Z-]+)$)
It seems to be working, but I am not sure if this is good enough, does it have some failure cases, etc...
它似乎在工作,但我不确定这是否足够好,它是否有一些失败的案例,等等……
I attempted to give it a shot at a few examples that should not be valid, but it seems to be bad...
我试着给它举几个不应该有效的例子,但它似乎很糟糕……
dfra // good
dfra- // good
dfr-Q // good
qwe-asd // good
dfr--asd // good
dfr-as2dqw // good
dfr--base--dog // good
dfr--root--x // fails (it should not pass with double dash of two keywords)
dfr-x1223 // good
Any help is appreciated. I am struggling so hard over here ???? !
任何帮助都是感激。我努力挣扎在这里????!
UPDATE #1:
UPDATE # 1:
I have just updated my regex a little bit and it seems to be better, not quite sure yet.
我刚刚更新了我的regex,它似乎更好,还不是很确定。
UPDATE #2:
更新2:
Here are some examples of what it should be able to validate and what it should not be able to validate:
下面是一些示例,说明它应该能够验证哪些内容,以及它不应该验证的内容:
// Should pass
dfr-borderRadius-extraSmall
dfr-borderRadius-full
dfr--base-color-dark
dfr--root-fontSize-small
dfr--base-fontSize
dfr-borderWidth
// Should fail
not-dfr
not-dfr-asd
dfr-1
dfr-@
dfr--a
dfr--base
dfr-root-
dfr--x-1
dfr--root--x
dfr--root-Something
dfr-border--x
dfr-asd-Asd
1 个解决方案
#1
2
You can do it with this pattern:
你可以使用以下模式:
^dfr(?:--(?:base|root|[xy]))?(?:-(?!base\b|root\b|[xy]\b)[a-z]+(?:[A-Z][a-z]+)*)+$
演示
details:
细节:
^dfr
(?:--(?:base|root|[xy]))? # optional keyword
(?: # group for other parts
-
(?!base\b|root\b|[xy]\b) # negative lookahead (not followed by a keyword)
[a-z]+(?:[A-Z][a-z]+)* # a camelCase word
)+ # repeat one or more times
$
Note that a word-boundary suffices to check if a word is followed by a dash or the end of the string, since only letters and dashes are allowed in the string.
注意,单词边界足以检查一个单词后面是否有破折号或字符串的末尾,因为字符串中只允许字母和破折号。
#1
2
You can do it with this pattern:
你可以使用以下模式:
^dfr(?:--(?:base|root|[xy]))?(?:-(?!base\b|root\b|[xy]\b)[a-z]+(?:[A-Z][a-z]+)*)+$
演示
details:
细节:
^dfr
(?:--(?:base|root|[xy]))? # optional keyword
(?: # group for other parts
-
(?!base\b|root\b|[xy]\b) # negative lookahead (not followed by a keyword)
[a-z]+(?:[A-Z][a-z]+)* # a camelCase word
)+ # repeat one or more times
$
Note that a word-boundary suffices to check if a word is followed by a dash or the end of the string, since only letters and dashes are allowed in the string.
注意,单词边界足以检查一个单词后面是否有破折号或字符串的末尾,因为字符串中只允许字母和破折号。