2

I'm trying to match the following text:

void foo( int xxx,
int const & xxx,

With the regex:

\(\s-+\)[^\s-]+,$

IE: match whitespace, followed by non-whitespace, followed by a single comma at the end of the line

Expected matches:

[space]xxx, (line 1)
[space]xxx, (line 2)

Actual matches:

[space]foo( int xxx, (line 1)
[space]& xxx, (line 2)

Why is emacs matching the spaces midstring even though I've specified 'no space'?

2 Answers 2

4

The character alternative, [^\s-] matches anything other than \, s, and -.

See "[ ... ]" in C-h i g (elisp) Regexp Special RET.

Instead of that, you want to use \S- (upper-case) as the inverse of \s-
(C-h i g (elisp) Regexp Backslash RET).

Or, if you still want/need a character alternative, use character classes
(C-h i g (elisp) Char Classes RET).

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

Comments

0

It's because you have not anchored the regexp to the beginning of the line. So in your two later cases:

[space]foo([space]int[space]xxx, (line 1)
[space]&[space]xxx, (line 2)

It simply matches [space]xxx,.

Try ^\(\s-+\)[^\s-]+,$ (or, when written as an Emacs Lisp string: "^\\(\\s-+\\)[^\\s-]+,$")

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.