views:

614

answers:

6

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

A: 
/^[^-]*-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
Quassnoi
A: 

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
soulmerge
This would allow more dashes later in the string, is that okay?
Joel Coehoorn
dashes are allowed only at the begining of a word. so if there's a space further on there can be dases after it. but then the same rule should apply to them
Mladen Prajdic
This would also only match a dash and one other character. ie it would match -m out of -myString.
Martin Brown
The idea is to pluck matches out of a larger string, so the regex can't be anchored and it has to match the whole word, not just the first letter.
Alan Moore
A: 

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

Joel Coehoorn
According to OP's edit, a sub string match is required, so anchors won't work.
Tomalak
Updated for the edit. The only question is how he decides how to end the "mystring" portion. This example uses whitespace.
Joel Coehoorn
I'm afraid now it is completely broken. ;-) I racked my brain for a simple no-look-behind-required solution as well, but I don't think there is one, when matching on a complete string is required.
Tomalak
+8  A: 

Assuming your regex engine supports (negative) lookbehind:

/(?<!-)-myString/

Perl does, Javascript doesn't, for example.

bart
my regex machine is the one in .Net. No clue if it supports that.
Mladen Prajdic
It does. "(?<!-)-\w+" would be the generic form, assuming that word characters (letters, numbers, underscore) is all that can legally follow the dash in your scenario.
Tomalak
Tomalak, yep that's exactly what i needed. how about you put that as an answer so i can mark it accepted?
Mladen Prajdic
@Mladen Prajdic: No, that would be a bit unfair. Bart's solution is correct, posting a trivial improvement to get an "accepted" check is stealing. ;-) @Bart, would you add the improved version to your answer?
Tomalak
Are the / / needed (or even valid) in .Net?
Svish
A: 

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

\b\-\w+
Mohamed Ali
A: 

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)
)
Neil Monroe