aspose file tools
The moose likes Beginning Java and the fly likes regex : match any character ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "regex : match any character ?" Watch "regex : match any character ?" New topic
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
    
  19

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: regex : match any character ?
 
Similar Threads
Filtering Files
Regular Expression - Predefined Character Class (.)
Problem with Regex
doubt on group() in Matcher class
Paragraph with dynamic content