字符类减法,从Java语法转换为RegexBuddy [英] Character class subtraction, converting from Java syntax to RegexBuddy
问题描述
Java使用哪个正则表达式引擎?
Which regular expression engine does Java uses?
如果我使用RegexBuddy之类的工具
In a tool like RegexBuddy if I use
[a-z&&[^bc]]
Java中的表达式很好,但是在RegexBuddy中尚未理解.
that expression in Java is good but in RegexBuddy it has not been understood.
实际上它报告:
匹配出现在其中的单个字符
[a-z&&[^bc]
Match a single character present in the list below
[a-z&&[^bc]
-
a
和z
之间的字符:a-z
- 字符
&[^bc
之一:&&[^bc
- 从字面上匹配字符
]
:]
- A character in the range between
a
andz
:a-z
- One of the characters
&[^bc
:&&[^bc
- Match the character
]
literally :]
但我想匹配a
和z
之间的字符,该字符与不是b
或c
but i want to match a character between a
and z
intersected with a character that is not b
or c
推荐答案
Like most regex flavors, java.util.regex.Pattern
has its own specific features with syntax that may not be fully compatible with others; this includes character class union, intersection and subtraction:
[a-d[m-p]]
:a
至d
,或m
至p
:[a-dm-p]
(联合)[a-z&&[def]]
:d
,e
或f
(交叉点)[a-z&&[^bc]]
:a
至z
,除了b
和c
:[ad-z]
(减法)
[a-d[m-p]]
:a
throughd
, orm
throughp
:[a-dm-p]
(union)[a-z&&[def]]
:d
,e
, orf
(intersection)[a-z&&[^bc]]
:a
throughz
, except forb
andc
:[ad-z]
(subtraction)
Java正则表达式最重要的"caveat"是matches
尝试将模式与整个字符串匹配.这对于大多数引擎而言都是非典型的,有时可能会引起混乱.
The most important "caveat" of Java regex is that matches
attempts to match a pattern against the whole string. This is atypical of most engines, and can be a source of confusion at times.
- regular-expressions.info/Flavor比较和减法允许您将Java中的所有辅音"定义为
[a-z&&[^aeiou]]
.Subtraction allows you to define for example "all consonants" in Java as
[a-z&&[^aeiou]]
.此语法特定于Java.在XML Schema,.NET,JGSoft和RegexBuddy中,它是
[a-z-[aeiou]]
.其他口味可能根本不支持此功能.This syntax is specific to Java. In XML Schema, .NET, JGSoft and RegexBuddy, it's
[a-z-[aeiou]]
. Other flavors may not support this feature at all.- regular-expressions.info/Character Classes in XML Regular Expressions
- MSDN - Regular Expression Character Classes - Subtraction
这篇关于字符类减法,从Java语法转换为RegexBuddy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!