Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression which matches space or nothing

Tags:

java

regex

I have JTextPane which colors "int" words blue. Such regexp is wrong because it will also color "print":

int + "(\\[\\])*" //To match eg. int[]

So I came on idea with such regexp:

"\\s" + int + "(\\[\\])*"

Its okay but doesnt work if user types int as first in text pane. How to solve this problem? Is there some symbol for NOTHING? So i could make: \s | NOTHING

like image 701
user2102972 Avatar asked Mar 04 '13 17:03

user2102972


2 Answers

Just match int surrounded by word boundaries, which are matched by \b. The pattern:

"\\bint\\b"

More reading over at the always-excellent regular-expressions.info.

like image 71
Matt Ball Avatar answered Oct 12 '22 22:10

Matt Ball


Do you want an optional space? That would be \\s?. Or to allow zero or more spaces: \\s*.

like image 35
Ryan Stewart Avatar answered Oct 12 '22 22:10

Ryan Stewart