I would like to match a word (that can contain any printable characters except space) separated on the left side by whitespace and on the right side by a newline or whitespace.

What is the difference between these two regex's:

  1. [[:space:]]*\\([^[:space:]]+?\\)\\(?:[[:space:]]|$\\)
  2. [[:space:]]*\\([[:graph:]]*\\)

And why does the first one not work?


In the first one, the or isn't escaped. I prefer a less fancy regex:

"\\s-\\([^\n\t ]+\\)[ \n\t]"

But it's a matter of taste, I guess.

  • Yes, I realized that I forgot to put "\\" in front of |.. thanks. But what if a line ends with "\r\n" like in DOS? – Håkon Hægland Dec 28 '14 at 14:53
  • Is "[[:space:]]" exactly equal to "\\s-" ? – Håkon Hægland Dec 28 '14 at 15:01
  • Yes, it's equal. And you could use [ \n\t\r]. – abo-abo Dec 28 '14 at 15:01
  • @HåkonHægland you will only get ^R in a buffer, if Emacs wasn't able to figure out file's encoding, but if all went ok, even in MS DOS encoded files \n will match the line end. – wvxvw Dec 28 '14 at 15:06
  • 1
    @HåkonHægland nope, you simply never get that character, if the file was parsed properly, Emacs pretends all line ends are just \n. You would need to specifically C-q c-r to get that character in an otherwise valid file. – wvxvw Dec 28 '14 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.