字母在 abcd 范围内的正则表达式至少出现一次

发布于 2024-10-27 22:13:29 字数 356 浏览 1 评论 0原文

您将如何编写一个正则表达式来返回包含每个字母序列(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以畏孤独 2024-11-03 22:13:29

使用前瞻检查单词中所有字母是否存在的简单模式:

\b(?=\w*a)(?=\w*b)(?=\w*c)(?=\w*d)\w+\b

您可能需要 ^...$ 而不是 \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:

\b(?=\w*a)(?=\w*b)(?=\w*c)(?=\w*d)\w+\b

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 an a 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

隔岸观火 2024-11-03 22:13:29
\b\w*abcd\w*\b

此正则表达式匹配包含 abcd 字符序列的所有单词。

\b\w*abcd\w*\b

This regex matches all words containing abcd character sequence..

爱给你人给你 2024-11-03 22:13:29

如果abcd必须按此顺序出现,则以下模式
将匹配:

{lc}*a{lc}*b{lc}*c{lc}*d{lc}*   { /* some code */ }

如果没有这样的顺序约束,正如 Kobi 回答的那样,前瞻将是
需要。
虽然flex有
尾随上下文,有限制
并且无法满足复杂的前瞻目的。

If a, b, c and d have to appear in this order, the following pattern
will match:

{lc}*a{lc}*b{lc}*c{lc}*d{lc}*   { /* some code */ }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文