vbs regex: match the text between ONLY the FIRST 2 underscores
I'm doing some filename scraping with VBS, if the file name was called
hello_world_2012_is_not the end of the world.pdf
then the regex should match the word "world" and nothing else.
I tried this:
[_][^_]+(?开发者_运维知识库=_)
but it gets everything that is between two underscores. How do I only select the first occurrence?
I suggest the following regex:
/^[^_]*_([^_]*)_/
Explanation:
^ # Anchor the search to the start of the string
[^_]* # Match any number of non-underscores, don't capture them
_ # Match the first underscore
([^_]*) # Match any number of non-underscores, do capture them
_ # Match the second underscore.
Then the first capturing group ($1
) will contain world
, and the regex won't match anywhere else in the string.
The regex itself should look something like this:
_([^_]*)_
The string is captured into group 1.
Alternatively, use string functions to locate the first 2 underscores and then extract what's between them.
精彩评论