字母在 abcd 范围内的正则表达式至少出现一次
您将如何编写一个正则表达式来返回包含每个字母序列(例如 abcd)至少一次的所有单词(除了所需的子序列之外还可能包含其他字母)?
abcd 必须是该单词的子序列。
谢谢!
Ps 与 lex 一起使用
%{
%}
delim [ \t\n]
ws {delim}+
lc [a-z]
%%
{ws} {/* no action taken */}
(?={lc}*a)(?={lc}*b)(?={lc}*c)(?={lc}*d) { /* some code */ }
%%
How would you write a regular expression that will return all words that contain each of a sequence of letters (such as abcd) at least once (may also contain other letters besides the required subsequence)?
abcd must be a subsequence of the word.
Thanks!
P.s. for use with lex
%{
%}
delim [ \t\n]
ws {delim}+
lc [a-z]
%%
{ws} {/* no action taken */}
(?={lc}*a)(?={lc}*b)(?={lc}*c)(?={lc}*d) { /* some code */ }
%%
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用前瞻检查单词中所有字母是否存在的简单模式:
您可能需要
^...$
而不是\b...\b
如果您想要验证该单词,而不是捕获它,并且您可能希望将\w
更改为您可接受的字母表。每个
(?=\w*a)
标记都是一个先行 - 它检查前面是否有字母和a
,但它不会前进 - 下一个条件 < code>b,再次从头开始检查。在我们检查完所有字母后,最后一个\w+
是实际捕获单词的内容。工作示例:http://rubular.com/r/L00DTpE813
另请参阅:前向和后向零宽度断言
A simple pattern to check the existence of all letters in a word, using lookaheads:
You may want
^...$
instead of\b...\b
if you want to validate the word, rather than capture it, and you may want to change\w
to your acceptable alphabet.Each
(?=\w*a)
token is a lookahead - it checks there are letters and ana
ahead, but it doesn't advance - the next condition,b
, is checked from the start again. The last\w+
is what actually captures the word, after we've checked all letters are there.Working example: http://rubular.com/r/L00DTpE813
See also: Lookahead and Lookbehind Zero-Width Assertions
此正则表达式匹配包含
abcd
字符序列的所有单词。This regex matches all words containing
abcd
character sequence..如果
a
、b
、c
和d
必须按此顺序出现,则以下模式将匹配:
如果没有这样的顺序约束,正如 Kobi 回答的那样,前瞻将是
需要。
虽然flex有
尾随上下文,有限制,
并且无法满足复杂的前瞻目的。
If
a
,b
,c
andd
have to appear in this order, the following patternwill match:
If there isn't such order constraint, as Kobi answered, look-ahead will be
needed.
Though flex has
trailing context, there are limitations,
and won't meet complex look-ahead purpose.