开发者

Java and .Net Regular Expressions

Difference between Java & .Net Framework Regular Expressions Pattern

I am trying to convert My .Net Framework but patterns are not valid

Can any 开发者_高级运维one point out the major differences in regex patterns

e.g. How would we name the grouping constructs in java, etc.


There are many differences that are summarised here.

The most important ones are:

  1. In Java strings, you need to escape all the backslashes (@"\s" becomes "\\s")
  2. Java does not support named capturing groups
  3. Java does not support infinite repetition inside lookbehinds.
  4. Java does not support conditionals ((?(?=regex)then|else))
  5. Unicode properties are named differently.

Most other differences are minor. One difference that is not mentioned above is Java's lack of support for balanced (recursive) regexes which I hope you don't have to use, ever.

If you need to convert lots of complicated regexes, consider investing in RegexBuddy which will do that for you.


... and one practical info. Im not sure what the point "Java does not support infinite repetition inside lookbehinds" means, but AFAIK and what I did test right now, .NET looks for match in the substring (somewhere), but Java needs to fit the patter from the origin of the source string.

Fast example:

task: is file name word file? (example demo.docx)

.NET solution: \.docx$ (this will success on "demo.docx", because pattern is found somewhere in the filename

Java solution: .*\.docx$ (you need to specify prefix .* to ensure that pattern can start anywhere in the file. The .NET pattern will not work in Java implementation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜