Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

regex - Character class subtraction, converting from Java syntax to RegexBuddy

Which regular expression engine does Java uses?

In a tool like RegexBuddy if I use

[a-z&&[^bc]]

that expression in Java is good but in RegexBuddy it has not been understood.

In fact it reports:

Match a single character present in the list below [a-z&&[^bc]

  • A character in the range between a and z : a-z
  • One of the characters &[^bc : &&[^bc
  • Match the character ] literally : ]

but i want to match a character between a and z intersected with a character that is not b or c

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 through d, or m through p: [a-dm-p] (union)
  • [a-z&&[def]] : d, e, or f (intersection)
  • [a-z&&[^bc]] : a through z, except for b and c: [ad-z] (subtraction)

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.

See also


On character class subtraction

Subtraction allows you to define for example "all consonants" in Java as [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.

References

Related questions


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...