0

I'm trying to select a substring using regex and I'm going round in circles. I need to select everything before the first "_".

exampale URL - GI_2013_JUNE_10_VOL3_LASTCHANCE

So the result Im looking for from the URL above would be "GI". The text before the first "_" can vary in length.

Any help would be much apprecited

1
  • 1
    Why not just use a substring function? Commented Jun 11, 2013 at 15:11

2 Answers 2

1

The regex would be:

^[^_]+

and grab the whole regex match. But as a comment says, using a substring function is more efficient!

Sign up to request clarification or add additional context in comments.

2 Comments

this has to be regex because its a web analytics tool looking at URl's
I really don't see the relationship?
0
^[^_]*

...is the expression you're looking for.

It basically says: Select everything that is not an underscore, starting at the beginning of the string.

http://regexr.com?356in

2 Comments

Good point. @user2335923 if the string does not contain an underscore, then use ^[^_]* if you want to match the entire string, or ^[^_]+ if you want to match nothing. Here's an updated regexr example: regexr.com?356it
Ihad to tweak it slightly but the code which worked for me was ([^_]+)_.*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.