Earliest instance of a character using Regex

  • Thread starter Thread starter trashman.horlicks
  • Start date Start date
T

trashman.horlicks

Hi all,
More regex questions, sadly ;)

If I had a string that could contain any combination, or some/none of
[].+-*/ how could I strip off extraneous characters after any of these
characters to get just the left most sub string?
eg
Do.Bears+Defecate*Rurally would give Do
Mostly*Harmless would give Mostly
Don-t/Panic would give Don

TIA

Paul
 
Hi all,
More regex questions, sadly ;)

If I had a string that could contain any combination, or some/none of
[].+-*/ how could I strip off extraneous characters after any of these
characters to get just the left most sub string?
eg
Do.Bears+Defecate*Rurally would give Do
Mostly*Harmless would give Mostly
Don-t/Panic would give Don

TIA

Paul

I believe it's easier using string methods:

s.Substring(0, s.IndexOfAny("[].+*/".ToCharArray()))
 
Hi all,
More regex questions, sadly ;)
If I had a string that could contain any combination, or some/none of
[].+-*/ how could I strip off extraneous characters after any of these
characters to get just the left most sub string?
eg
Do.Bears+Defecate*Rurally would give Do
Mostly*Harmless would give Mostly
Don-t/Panic would give Don

Paul

I believe it's easier using string methods:

s.Substring(0, s.IndexOfAny("[].+*/".ToCharArray()))

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -


Thanks mate!
 
Hello trashman,
If I had a string that could contain any combination, or some/none of
[].+-*/ how could I strip off extraneous characters after any of these
characters to get just the left most sub string?
eg
Do.Bears+Defecate*Rurally would give Do
Mostly*Harmless would give Mostly
Don-t/Panic would give Don

You could use this expression:

.*?[\[\]\.+\-*/]

Looks wild - you're scanning for characters that have special meaning in
regular expressions. Also, look only for the first match, as you're not
interested in the rest.

I agree with Göran's suggestion of using string functions instead - not
necessarily because it's easier, I think that's a matter of content and
personal perception, but because regular expressions always incur a
certain overhead. As a basic rule, if there's no particular reason to want
the regular expression, an alternative solution will usually be preferable
to me.


Oliver Sturm
 
Back
Top