2

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!

2 Answers 2

1

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.

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

5 Comments

Yes indeed, very useful! However, it turns out, it wasn't really my regex that was faulty but rather that I use then inside the function (s-contains? "REGEX" (buffer-substring beg-of-line end-of-line)) where the variables are the beginning/end of the current line. This does not support regular expressions. I will accept your answer because of the suggestion of re-builder and because of pointing out the problem with the whitespace class.
Btw, I usually prefer buffer-substring-no-properties. Not sure if it's faster, but it probably is. And more readable when debugging.
Just a note, if you prefer the expression to match only a single space instead of any whitespace (tab), use [C-q SPC] instead of [\s-].
abo-abo: [\s-] matches any of backslash, lower-case s, or hyphen. I don't think you wanted any square brackets there.
@phis, actually it was because of re-builder. It actually thinks that [\s-] matches a space.
0

Could be an emacs bug?

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

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

1 Comment

Emacs has a different regex syntax

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.