how do i do this with regex?
我如何用正则表达式做到这一点?
i want to match this string: -myString
我想匹配这个字符串:-myString
but i don't want to match the -myString
in this string: --myString
但我不想匹配此字符串中的-myString: - myString
myString is of course anything.
myString当然是任何东西。
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
这个正则表达式返回3个匹配:-string1, - 和-string2
ideally i'd like it to return me only the -string1
match
理想情况下,我希望它只返回-string1匹配
6 个解决方案
#1
Assuming your regex engine supports (negative) lookbehind:
假设你的正则表达式引擎支持(负面)lookbehind:
/(?<!-)-myString/
Perl does, Javascript doesn't, for example.
Perl确实如此,Javascript没有。
#2
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
#3
/^[^-]*-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
#4
based on the last edit, I guess the following expression would work better
根据上次编辑,我猜以下表达式会更好
\b\-\w+
#5
[^-]{0,1}-[^\w-]+
#6
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)
)
#1
Assuming your regex engine supports (negative) lookbehind:
假设你的正则表达式引擎支持(负面)lookbehind:
/(?<!-)-myString/
Perl does, Javascript doesn't, for example.
Perl确实如此,Javascript没有。
#2
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
#3
/^[^-]*-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
#4
based on the last edit, I guess the following expression would work better
根据上次编辑,我猜以下表达式会更好
\b\-\w+
#5
[^-]{0,1}-[^\w-]+
#6
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)
)