.NET to Java Regular Expression
I am converting from .NET to Java and the following .NET Regular expression fai开发者_C百科ls.
(?<before>.{0,10})" + mSearchTerm + "(?<after>.{0,255})
There are 2 named groups here, but the named part is not important to me.
The named groups are the only thing I see that won't work in Java, but you seem to have left off some quotation marks. Try this:
Pattern p = Pattern.compile("(.{0,10})" + mSearchTerm + "(.{0,255})");
In addition to the answer given by Alan Moore, the upcoming jdk7 will support named groups in regular expressions. See http://download-llnw.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html for details.
Also, if you are building a regex from a searchstring that is not a regex itself, it would be better to use Pattern.quote(searchString)
, so that all special characters are properly escaped.
精彩评论