tags:

views:

52

answers:

3

I want to capture everything up to (not including) a # sign in a string. The # character may or may not be present (if it's not present, the whole string should be captured).

What would the RegEx and C# code for this by? I've tried: ([^#]+)(?:#) but it doesn't seem to work.

+2  A: 

Try:

.*(?=#)

I think that should work

EDIT:

^[^#]*

In code:

string match = Regex.Match(input,"^[^#]*").Value;
Philippe Leybaert
Not quite, that will match everything up to the last # .. I think the op wants everything up to the first #
Michael Petito
@Michael Petito: So... add a `?` after the `*`
R. Bemrose
@OMG Unicorns because using a lazy quantifier in this case just seems, well, lazy!
Michael Petito
+2  A: 

Not a regex but an alternative to try. A regex can be used though, but for this particular situation I prefer this method.

string mystring = "DFASDFASFASFASFAF#322323"; 
int length = (mystring.IndexOf('#') == -1) ? mystring.Length : mystring.IndexOf('#');
string new_mystring = mystring.Substring(0, length);
gmcalab
I'd prefer this - sometimes there's a simpler solution and regular expressions just aren't needed.
FrustratedWithFormsDesigner
This doesn't look simpler to me...
Philippe Leybaert
I think it's more readable.
gmcalab
You're kidding, right?
Philippe Leybaert
I agree with Philippe the regex version is far more readable.Though the regex version is only readable to people who knows how to read regex.
JohannesH
http://www.regular-expressions.info/ is always a good start.:)
JohannesH
@JohannesH: that's true, but that can easily be solved by a single comment line in the code
Philippe Leybaert
I would prefer the regular expression in this case. My guess is this regex is actually being used to strip end of line comments indicated by #, as are commonly used in configuration files. If so, a more complete definition of the expression might strip trailing whitespace and this would be harder to implement (and less clear) using IndexOf and Substring.
Michael Petito
+1  A: 

What's wrong with something as simple as:

[^#]*

Just take the first match?

Michael Petito