Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent a single space character in a square bracket group

Tags:

regex

I'm facing an issue to represent a single space in the [] character set expression. I ended up using something like this

([a-zA-Z0-9]|\x20)+

which i think is not very elegant

Update: I now know what is going wrong with my regex. I used something like this which did not work

[A-Za-z0-9/.,':!?$%()- ]+

But when I move the space to be after 9 like this

[A-Za-z0-9 /.,':!?$%()-]+

And it works, just weird. I'm trying this with an online regex tester RegExr.

Update2: I figured out now the issue is that - inside regex is used to represent a range in [] so anything before and after it has different meaning than just being a plain character

like image 953
imgen Avatar asked Jan 16 '13 08:01

imgen


People also ask

How do you represent a space in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

What is a single space character?

When rendered, a whitespace character does not correspond to a visible mark, but typically does occupy an area on a page. For example, the common whitespace symbol U+0020 SPACE (also ASCII 32) represents a blank space punctuation character in text, used as a word divider in Western scripts.

What do the [] brackets mean in regular expressions?

Square brackets ( “[ ]” ): Any expression within square brackets [ ] is a character set; if any one of the characters matches the search string, the regex will pass the test return true.

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.


2 Answers

You can just use a space.

For the regex structure in the question:

 ([a-zA-Z0-9]| )+

In practice:

[a-zA-Z0-9 ]+
like image 117
AD7six Avatar answered Oct 07 '22 15:10

AD7six


[a-zA-Z0-9 ]

will add a space to the character class. I'm guessing you already know that and you want to make sure that there will be only single spaces in your string. In that case, you need an additional lookahead:

^(?!.*  )[a-zA-Z0-9 ]*$

matches a string that contains alphanumerics and spaces, but only single spaces.

like image 45
Tim Pietzcker Avatar answered Oct 07 '22 15:10

Tim Pietzcker