我无法弄清楚的正则表达式问题(负向后查找)

发布于 2024-07-13 05:30:08 字数 485 浏览 9 评论 0原文

我如何使用正则表达式做到这一点?

我想匹配这个字符串:-myString

但我不想匹配这个字符串中的-myString--myString

myString是当然任何东西。

有可能吗?

编辑:

这是自从我发布问题以来到目前为止我得到的更多信息:

string to match:
some random stuff here -string1, --string2, other stuff here
regex:
(-)([\w])*

这个正则表达式返回我 3 个匹配项: -string1--string2

理想情况下,我希望它只返回 -string1 匹配项

how do i do this with regex?

i want to match this string: -myString

but i don't want to match the -myString in this string: --myString

myString is of course anything.

is it even possible?

EDIT:

here's a little more info with what i got so far since i've posted a question:

string to match:
some random stuff here -string1, --string2, other stuff here
regex:
(-)([\w])*

This regex returns me 3 matches:
-string1, - and -string2

ideally i'd like it to return me only the -string1 match

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

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

发布评论

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

评论(6

甜柠檬 2024-07-20 05:30:08

假设您的正则表达式引擎支持(负)lookbehind:

/(?<!-)-myString/

例如,Perl 支持,Javascript 不支持。

Assuming your regex engine supports (negative) lookbehind:

/(?<!-)-myString/

Perl does, Javascript doesn't, for example.

梦归所梦 2024-07-20 05:30:08

您想要匹配以单个破折号开头的字符串,而不是具有多个破折号的字符串?

^-[^-]

解释:

^ Matches start of string
- Matches a dash
[^-] Matches anything but a dash

You want to match a string that starts with a single dash, but not one that has multiple dashes?

^-[^-]

Explanation:

^ Matches start of string
- Matches a dash
[^-] Matches anything but a dash
怎言笑 2024-07-20 05:30:08
/^[^-]*-myString/

测试:

[~]$ echo -myString | egrep -e '^[^-]*-myString'
-myString
[~]$ echo --myString | egrep -e '^[^-]*-myString'
[~]$ echo test--myString | egrep -e '^[^-]*-myString'
[~]$ echo test --myString | egrep -e '^[^-]*-myString'
[~]$ echo test -myString | egrep -e '^[^-]*-myString'
test -myString
/^[^-]*-myString/

Testing:

[~]$ echo -myString | egrep -e '^[^-]*-myString'
-myString
[~]$ echo --myString | egrep -e '^[^-]*-myString'
[~]$ echo test--myString | egrep -e '^[^-]*-myString'
[~]$ echo test --myString | egrep -e '^[^-]*-myString'
[~]$ echo test -myString | egrep -e '^[^-]*-myString'
test -myString
暮年 2024-07-20 05:30:08

根据上次编辑,我想以下表达式会更好

\b\-\w+

based on the last edit, I guess the following expression would work better

\b\-\w+
纸伞微斜 2024-07-20 05:30:08

[^-]{0,1}-[^\w-]+

[^-]{0,1}-[^\w-]+

决绝 2024-07-20 05:30:08

在不使用任何后视的情况下,使用:

(?:^|(?:[\s,]))(?:\-)([^-][a-zA-Z_0-9]+)

打破:

(
  ?:^|(?:[\s,])        # Determine if this is at the beginning of the input,
                       # or is preceded by whitespace or a comma
)
(
  ?:\-                 # Check for the first dash
)
(
  [^-][a-zA-Z_0-9]+    # Capture a string that doesn't start with a dash
                       # (the string you are looking for)
)

Without using any look-behinds, use:

(?:^|(?:[\s,]))(?:\-)([^-][a-zA-Z_0-9]+)

Broken out:

(
  ?:^|(?:[\s,])        # Determine if this is at the beginning of the input,
                       # or is preceded by whitespace or a comma
)
(
  ?:\-                 # Check for the first dash
)
(
  [^-][a-zA-Z_0-9]+    # Capture a string that doesn't start with a dash
                       # (the string you are looking for)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文