Regular Expression to Find Spaces

Aldo picture Aldo · Jun 6, 2013 · Viewed 78.7k times · Source

I have the following text = "superilustrado e de capa dura?", and I want to find all the spaces between words in the text. I am using the following expression = [\\p{L}[:punct:]][[:space:]][\\p{L}[:punct:]]. The expression works fine but it can find the space between the "e de". Does anybody know what is the problem with my regular expression?

Answer

user1919238 picture user1919238 · Jun 6, 2013

Spaces can be found simply by putting a space character in your regex.

Whitespace can be found with \s.

If you want to find whitespace between words, use the \b word boundary marker.

This would match a single space between two words:

"\b \b"

(The reason your match failed is that \\p{L} includes the character in a match. Because e is only one character, it gets eaten up by the previous match and can't be matched for the space after e. \b avoids this problem because it is a zero-width match.)