1

I have a string like following,

hi,hello,-LSB-,ASPECT,-RSB-,you

I want to extract sub-string that comes before -LSB-,ASPECT, till comma, hello in this case.

I have written regular expression like

\b\w+[/-/,LSB/-/,ASPECT]

however it extracts entire substring before and inclusing-LSB-,ASPECT, till start like,

hi,hello,-LSB-,ASPECT

Any clue??

2
  • Why are you trying to use a regex for this? It's a simple case of using SubString() and IndexOf(); there's no need at all for a regex. Commented Jun 7, 2014 at 15:19
  • there may be many "," or words before -LSB-,ASPECT , also -LSB-,ASPECT can be any where in this big string, this is just example. Commented Jun 7, 2014 at 15:26

2 Answers 2

3

The regex for this (using a positive lookahead assertion) would be

[^,]*(?=,-LSB-,ASPECT,)

Explanation:

[^,]*            # Match any number of characters except commas
(?=              # until the following regex can be matched:
 ,-LSB-,ASPECT,  # the literal text ",-LSB-,ASPECT,".
)                # (End of lookahead assertion)

Careful, square brackets create a character class which you don't want in this case.

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

2 Comments

@TimPietzceker my mistake apologies you're right +1
looks good in expresso, but also returning a NULL character too there. Will put in my actual code ....
0

Live demo

Try this:

(\w+),-LSB-,ASPECT

Comments

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.