简体   繁体   中英

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

eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM