elemakil
elemakil

Reputation: 3811

Cannot match string with optional whitespace using regex

I'm trying to match the following strings:
/*virtual*/, /* virtual*/, /*virtual */ and /* virtual */

The regexp /\*\svirtual\s\*/ correctly matches the version with two spaces, however replacing \s by [\s]* does not match any of these strings... From reading the emacs RegularExpression reference document on the wiki I have assumed that this should create a regex matching my specification. I am still a bit new to regular expressions, any help (and explanation why the above is faulty) is welcome!

Upvotes: 2

Views: 407

Answers (2)

abo-abo
abo-abo

Reputation: 20342

Try re-builder. It's so useful. And the expression is

"/\\* ?virtual ?\\*/"

Or, if you like to go with s-,

"/\\*\\s-?virtual\\s-?\\*/"

But note that it's not exactly a space, but whatever the syntax defines to be whitespace. So I could write a mode that designates all words like as whitespace, and your regex would match them.

Upvotes: 1

Jim W
Jim W

Reputation: 5016

Could be an emacs bug?

\*[\s]*virtual[\s]*\*

works in JS, see http://gskinner.com/RegExr/ (check 'global')

Upvotes: 0

Related Questions