Python和Java之间RegEx语法的差异 [英] Differences in RegEx syntax between Python and Java

查看:134
本文介绍了Python和Java之间RegEx语法的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有一个正在运行的正则表达式,我正在尝试转换为Java。似乎实现中存在细微差别。

I have a working regex in Python and I am trying to convert to Java. It seems that there is a subtle difference in the implementations.

RegEx正试图匹配另一个注册表。有问题的RegEx是:

The RegEx is trying to match another reg ex. The RegEx in question is:

/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/([gim]+\b|\B)

遇到问题的其中一个字符串是: / \s + /;

One of the strings that it is having problems on is: /\s+/;

reg ex不应与结尾; 匹配。在Python中,RegEx正常工作(并且与结束; 不匹配,但在Java中它确实包含;

The reg ex is not supposed to be matching the ending ;. In Python the RegEx works correctly (and does not match the ending ;, but in Java it does include the ;.

问题:


  1. 我该怎么做才能得到这个RegEx在Java中工作?

  2. 基于我读到的内容这里这个RegEx应该没有什么区别。在Python和Java中的RegEx实现之间是否存在差异列表?

  1. What can I do to get this RegEx working in Java?
  2. Based on what I read here there should be no difference for this RegEx. Is there somewhere a list of differences between the RegEx implementations in Python vs Java?


推荐答案

对于一小组案例,Java不会像Python那样解析正则表达式。在这种特殊情况下,嵌套的 [导致问题。在Python中你不需要转义任何嵌套的 [但你确实需要在Java中做到这一点。

Java doesn't parse Regular Expressions in the same way as Python for a small set of cases. In this particular case the nested ['s were causing problems. In Python you don't need to escape any nested [ but you do need to do that in Java.

原始的RegEx(对于Python):

The original RegEx (for Python):

/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/([gim]+\b|\B)

固定的RegEx(适用于Java和Python):

The fixed RegEx (for Java and Python):

/(\\.|[^\[/\\\n]|\[(\\.|[^\]\\\n])*\])+/([gim]+\b|\B)

这篇关于Python和Java之间RegEx语法的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆