Every regular expression matching a space also matches the empty string after the space, but there is no way to find out where the empty string comes from. From the view point of a regexp engine [no matter what programming language] there are empty strings before and after every character in the string, so if you have a string
"ABC", then the regexp engine sees it as:
<start-of-string>
<empty-string>A<empty-string>B<empty-string>C<empty-string><end-of-string>
That's the reason why the ELisp manual writes:
\b - matches the
empty string,
but only at the beginning or end of a word.
\B - matches the
empty string,
but not at the beginning or end of a word.
The reason for this somewhat strange sounding definition is that from the view point of the regexp engine there are empty strings before and after every character in the string and the only way to find out if an empty string occurs at the beginning or end of a word is to look at the characters before and after the empty string between the characters.
The question is: which empty string do you want to match?
The particular problem in practice is that after concatenating two strings, the regexp engine has no chance to find out where the concatenation happened and if an empty string was concatenated or not. In such situations the only way is to use lists or vectors of strings instead of concatenation.
To match the empty string between a space and a non-space character or the empty string after a last space character in a string you could use:
- Code: Select all
(string-match " \\([^ ]\\|\\'\\)" "ABC") => NIL ; no space character
(string-match " \\([^ ]\\|\\'\\)" " ABC") => 0
(string-match " \\([^ ]\\|\\'\\)" "A BC") => 1
(string-match " \\([^ ]\\|\\'\\)" "AB C") => 2
(string-match " \\([^ ]\\|\\'\\)" "ABC ") => 3
(string-match " \\([^ ]\\|\\'\\)" "A B C") => 1
(string-match " \\([^ ]\\|\\'\\)" "A B ") => 2
(string-match " \\([^ ]\\|\\'\\)" "A B") => 3
This gives the position of the last space character before the empty string after the space.
Is that what you wanted?
- edgar