| Author |
regex : match any character ?
|
Edward Chen
Ranch Hand
Joined: Dec 23, 2003
Posts: 758
|
|
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
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Edward Chen
Ranch Hand
Joined: Dec 23, 2003
Posts: 758
|
|
|
Thank you very much.
|
 |
Piet Verdriet
Ranch Hand
Joined: Feb 25, 2006
Posts: 266
|
|
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.
|
 |
 |
|
|
subject: regex : match any character ?
|
|
|