简体   繁体   中英

regex to get substring before substring

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??

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.

Live demo

Try this:

(\w+),-LSB-,ASPECT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM