• Post Reply Bookmark Topic Watch Topic
  • New Topic

regex : match any character ?  RSS feed

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to replace "${(col=='sadfdsafds')?'':'selected'}" with a new string. I wrote a regex, like

Pattern.compile("\\$\\{ * \\}");

I use the star to match any character inside "{ }", it doesn't work, which expression I should use ?

Thanks.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use the star to match any character inside "{ }", it doesn't work, which expression I should use ?


That's not how it works. Regular expressions, and the pattern matching used by OSes, don't work the same. In a regular expression, a "." matches everything. A "*" means zero or more of the previous character. Also, a regular express will try to match as much as possible -- so if you want to match to the next close curly brace, instead of to the last close curly brace, you need to use the "?" modifier to tell the regex to be reluctant.

Basically, you should use ".*?" between the braces, to match the minimum amount of anything, between the braces.

Henry
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


That's not how it works. Regular expressions, and the pattern matching used by OSes, don't work the same. In a regular expression, a "." matches everything. ...


Except new line characters.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!