如何编写一个正则表达式来匹配所有不是 '$' 的字符接下来是“i”;或'{'?

发布于 2024-09-28 11:41:48 字数 605 浏览 2 评论 0原文

意思是,我想匹配:

$10

或,

$

但不是这个:

${name}

或:

$image{http://wrfgadgadga.com/gadgad.png}

我还想匹配其他所有内容...普通字符、符号、数字等。

匹配除以 $ 开头的内容之外的所有内容都很容易。就像这样:

def literalCharacter: Parser[String] = """[^\$]""".r

我已经尝试使用 (?!i) 或 (?!{) 的多种组合来使用正则表达式前瞻语法,但我似乎无法让它工作。我还尝试用 = 而不是 ! 重写它。像这样: (?=i)

基本上,我尝试用 [^\$] 表达式以我能想象的各种方式注入这些前瞻,但我无法让它工作。

帮助?

编辑:嗯,这似乎有效:

[^\$]|\$(?!i)|\$(?!\{)

Meaning, I want to match:

$10

or

$

but not this:

${name}

or:

$image{http://wrfgadgadga.com/gadgad.png}

I also want to match everything else... normal characters, symbols, numbers, etc.

Matching everything but things that start with $ is easy. It's like this:

def literalCharacter: Parser[String] = """[^\$]""".r

I've tried the regular expression look-ahead syntax using (?!i) or (?!{) in numerous combinations but I can't seem to get it to work. I've also tried rewriting it with the = instead of the ! like this: (?=i)

Basically, I've tried injecting these look-aheads in every way I can image with the [^\$] expression and I can't get it work.

Help?

EDIT: Hrm, this seems to work:

[^\$]|\$(?!i)|\$(?!\{)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

十年不长 2024-10-05 11:41:48

您的字符串无法正确匹配 x$ 等字符串。如果你想匹配整个字符串,那么尝试

"""^\$|^[^\$].*$|^\$[^i\{].*$"""

我们匹配由 | 分隔的三个序列中的任何一个:

^\$
^[^\$]+.*$
^\$[^i\{]+.*$

让我们把它拆开:

// First pattern--match lone '

如果你不匹配整个字符串,你必须担心像 foo$image{Hi} 这样的东西。如果您还想匹配空字符串,请在匹配项前面添加 ^$|

请注意,这是专门为正则表达式而编写的,而不是考虑到您的解析器组合器。根据您拥有的其他规则,您可能希望也可能不希望匹配整个字符串。

character ^ // Matches start of string \$ // Matches the literal character '

如果你不匹配整个字符串,你必须担心像 foo$image{Hi} 这样的东西。如果您还想匹配空字符串,请在匹配项前面添加 ^$|

请注意,这是专门为正则表达式而编写的,而不是考虑到您的解析器组合器。根据您拥有的其他规则,您可能希望也可能不希望匹配整个字符串。

$ // Matches end of string // Second pattern--match a string starting with a character other than '

如果你不匹配整个字符串,你必须担心像 foo$image{Hi} 这样的东西。如果您还想匹配空字符串,请在匹配项前面添加 ^$|

请注意,这是专门为正则表达式而编写的,而不是考虑到您的解析器组合器。根据您拥有的其他规则,您可能希望也可能不希望匹配整个字符串。

^ // Start of string [^\$]+ // Match at least one non-'

如果你不匹配整个字符串,你必须担心像 foo$image{Hi} 这样的东西。如果您还想匹配空字符串,请在匹配项前面添加 ^$|

请注意,这是专门为正则表达式而编写的,而不是考虑到您的解析器组合器。根据您拥有的其他规则,您可能希望也可能不希望匹配整个字符串。

: + // Match one or more [^ ] // ...of characters NOT listed... \$ // ...namely, a literal '

如果你不匹配整个字符串,你必须担心像 foo$image{Hi} 这样的东西。如果您还想匹配空字符串,请在匹配项前面添加 ^$|

请注意,这是专门为正则表达式而编写的,而不是考虑到您的解析器组合器。根据您拥有的其他规则,您可能希望也可能不希望匹配整个字符串。

.* // Any number of other characters $ // End of the string // Third pattern--match a string starting with '

如果你不匹配整个字符串,你必须担心像 foo$image{Hi} 这样的东西。如果您还想匹配空字符串,请在匹配项前面添加 ^$|

请注意,这是专门为正则表达式而编写的,而不是考虑到您的解析器组合器。根据您拥有的其他规则,您可能希望也可能不希望匹配整个字符串。

but not '$i' or '${' ^ // Start of string \$ // The literal character '

如果你不匹配整个字符串,你必须担心像 foo$image{Hi} 这样的东西。如果您还想匹配空字符串,请在匹配项前面添加 ^$|

请注意,这是专门为正则表达式而编写的,而不是考虑到您的解析器组合器。根据您拥有的其他规则,您可能希望也可能不希望匹配整个字符串。

[^i\{]+ // Match at least one non-'i'/non-'{' .* // Any number of other characters $ // End of the string

如果你不匹配整个字符串,你必须担心像 foo$image{Hi} 这样的东西。如果您还想匹配空字符串,请在匹配项前面添加 ^$|

请注意,这是专门为正则表达式而编写的,而不是考虑到您的解析器组合器。根据您拥有的其他规则,您可能希望也可能不希望匹配整个字符串。

Yours won't match strings like x$ properly. If you want to match the entire string, then try

"""^\$|^[^\$].*$|^\$[^i\{].*$"""

where we are matching either of three sequences separated by |:

^\$
^[^\$]+.*$
^\$[^i\{]+.*$

Let's take this apart:

// First pattern--match lone '

If you don't match the whole string, you have to worry about things like foo$image{Hi}. If you want to match the empty string also, prepend ^$| to the match.

Note that this is written to work specifically with regexes, not with your parser combinator in mind. Depending on what other rules you have, you may or may not want to match the whole string.

character ^ // Matches start of string \$ // Matches the literal character '

If you don't match the whole string, you have to worry about things like foo$image{Hi}. If you want to match the empty string also, prepend ^$| to the match.

Note that this is written to work specifically with regexes, not with your parser combinator in mind. Depending on what other rules you have, you may or may not want to match the whole string.

$ // Matches end of string // Second pattern--match a string starting with a character other than '

If you don't match the whole string, you have to worry about things like foo$image{Hi}. If you want to match the empty string also, prepend ^$| to the match.

Note that this is written to work specifically with regexes, not with your parser combinator in mind. Depending on what other rules you have, you may or may not want to match the whole string.

^ // Start of string [^\$]+ // Match at least one non-'

If you don't match the whole string, you have to worry about things like foo$image{Hi}. If you want to match the empty string also, prepend ^$| to the match.

Note that this is written to work specifically with regexes, not with your parser combinator in mind. Depending on what other rules you have, you may or may not want to match the whole string.

: + // Match one or more [^ ] // ...of characters NOT listed... \$ // ...namely, a literal '

If you don't match the whole string, you have to worry about things like foo$image{Hi}. If you want to match the empty string also, prepend ^$| to the match.

Note that this is written to work specifically with regexes, not with your parser combinator in mind. Depending on what other rules you have, you may or may not want to match the whole string.

.* // Any number of other characters $ // End of the string // Third pattern--match a string starting with '

If you don't match the whole string, you have to worry about things like foo$image{Hi}. If you want to match the empty string also, prepend ^$| to the match.

Note that this is written to work specifically with regexes, not with your parser combinator in mind. Depending on what other rules you have, you may or may not want to match the whole string.

but not '$i' or '${' ^ // Start of string \$ // The literal character '

If you don't match the whole string, you have to worry about things like foo$image{Hi}. If you want to match the empty string also, prepend ^$| to the match.

Note that this is written to work specifically with regexes, not with your parser combinator in mind. Depending on what other rules you have, you may or may not want to match the whole string.

[^i\{]+ // Match at least one non-'i'/non-'{' .* // Any number of other characters $ // End of the string

If you don't match the whole string, you have to worry about things like foo$image{Hi}. If you want to match the empty string also, prepend ^$| to the match.

Note that this is written to work specifically with regexes, not with your parser combinator in mind. Depending on what other rules you have, you may or may not want to match the whole string.

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