java - java 是否支持 if-then-else 正则表达式构造(Perl 构造)?

标签 java regex perl

我在尝试编译以下正则表达式时收到 PatternSyntaxException:

"bd".matches("(a)?b(?(1)c|d)")

这个正则表达式匹配 bd 和 abc。它与 bc 不匹配。

有什么想法吗?谢谢。

好的,我需要编写正则表达式来匹配接下来的 4 个字符串:

*date date* date date1*date2

不应该匹配:

*date* date1*date2* *date1*date2 date** ...

但这应该通过单个匹配来完成,而不是多个。

请不要发布如下答案:

(date*date)|(*date)|(date*)|(date)

最佳答案

想象一下,如果您可以使用一种缺少 else 的语言声明,但您想效仿它。而不是写作

if (condition) { yes part }
else           { no part  }

你必须写

if (condition)   { yes part }
if (!condition)  { no part  }

好吧,这就是你必须在这里做的,但在模式中。在没有条件的 Java 中,您所做的是在 ELSE block (实际上是 OR block )中重复条件,但否定它。

因此,例如,不要使用像 Perl 这样在模式中提供条件支持的语言来编写:

# definition of \b using a conditional in the pattern like Perl
#
(?(?<=      \w)     # if there is a word character to the left
      (?!   \w)     #    then there must be no word character to the right
  |   (?=   \w)     #    else there must be a  word character to the right
)

你必须用 Java 写:

# definition of \b using a duplicated condition like Java
#
(?:   (?<=  \w)     # if there is a word character to the left
      (?!   \w)     #    then there must be no word character to the right
  |                 # ...otherwise...
      (?<!  \w)     # if there is no word character to the left
      (?=   \w)     #    then there must be a word character to the right
)

您可能认为这是 \b 的定义.那么这里同样适用于 \B的定义,首先使用条件:

# definition of \B using a conditional in the pattern like Perl
#
(?(?<=      \w)     # if there is a word character to the left
      (?=   \w)     #    then there must be a  word character to the right
  |   (?!   \w)     #    else there must be no word character to the right
)

现在通过在 OR 分支中重复(现在否定的)条件:

# definition of \B using a duplicated condition like Java
#
(?:   (?<=  \w)     # if there is a word character to the left
      (?!   \w)     #    then there must be no word character to the right
  |                 # ...otherwise...
      (?<!  \w)     # if there is no word character to the left
      (?=   \w)     #    then there must be a word character to the right
)

请注意,无论您如何滚动它们,\b 的相应定义都是如此和 \B同样完全取决于 \w 的定义, 永远不会 \W ,更不用说 \s .

能够使用条件语句不仅可以节省打字时间,还可以减少出错的几率。它们也可能是您不关心对条件进行两次评估的情况。

在这里,我利用它来定义几个正则表达式子例程,这些子例程为我提供了希腊语原子和相同的边界:

(?(DEFINE)
    (?<greeklish>            [\p{Greek}\p{Inherited}]   )
    (?<ungreeklish>          [^\p{Greek}\p{Inherited}]  )
    (?<greek_boundary>
        (?(?<=      (?&greeklish))
              (?!   (?&greeklish))
          |   (?=   (?&greeklish))
        )
    )
    (?<greek_nonboundary>
        (?(?<=      (?&greeklish))
              (?=   (?&greeklish))
          |   (?!   (?&greeklish))
        )
    )
)

注意边界和非边界如何只使用 (&?greeklish) , 从不 (?&ungreeklish) ?你永远不需要非任何东西来做边界。你把 not 放到你的 lookarounds 中,就像 \b\B两者都有。

尽管在 Perl 中定义一个新的自定义属性可能更容易(尽管不那么通用),\p{IsGreeklish} (及其补充 \P{IsGreeklish} ):

 sub IsGreeklish {
     return <<'END';
 +utf8::IsGreek
 +utf8::IsInherited
 END
 }

尽管这不是因为 Java 缺乏对条件的支持,而是因为它的模式语言不允许 (DEFINE),但您将无法将其中任何一个翻译成 Java。 block 或正则表达式子例程调用,如 (?&greeklish) — 实际上,您的模式甚至不能在 Java 中递归。您也不能在 Java 中定义自定义属性,例如 \p{IsGreeklish} .

当然,Perl 正则表达式中的条件可以不仅仅是环顾四周:它们甚至可以是要执行的代码块——这就是为什么您当然不希望被迫对同一条件求值两次,以免产生副作用.这不适用于 Java,因为它做不到。您不能混合模式和代码,这对您的限制比您在养成这样做的习惯之前想象的要多。

使用 Perl 正则表达式引擎可以做很多其他语言做不到的事情,这只是其中的一部分。难怪新的第 4 版 Programming Perl 中大大扩展的 Regexes 章节,加上完全重写的 Unicode 章节,现在紧跟在 Regexes 章节之后(已提升为内核的一部分),有一个组合页数大约 130 页,因此是第 3 版中有关模式匹配的旧章节长度的两倍。

您刚才在上面看到的是新的第 4 版的一部分,应该会在下个月左右打印。

关于java - java 是否支持 if-then-else 正则表达式构造(Perl 构造)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8072756/

相关文章:

regex - 为什么这个正则表达式返回的组比它应该返回的多?

perl - 如何将 Plack::Middleware::CSRFBlock 与 Dancer 一起使用?

java - Apache POI 3.7 OutOfMemoryError : Java heap space when writing to large no of rows to xlsx files

java - Getter 不与任何字段关联

java - Querydsl - 在 pom.xml 中注册自定义类型

java - 正则可选匹配

javascript - 使用正则表达式和 Vue 处理字符串切片和格式化

java - 在 Java 中将字符串转换为日历对象

java - 只查找从特殊字符 java 开始的第一个单词

perl - 从 CGI 迁移到 mod_perl。了解我的、我们的、本地的