java.util.eclipse regex util.PatternSyntaxException 怎么解决哦

博客分类:
1.4 Java (java.util.regex)
Java 1.4 supports regular
expressions with Sun's java.util.regex
package. Although there are
competing packages available for previous versions of Java, Sun is poised to
become the standard. Sun's package uses a Traditional NFA match engine. For an
explanation of the rules behind a Traditional NFA engine, see
1.4.1 Supported Metacharacters
java.util.regex
supports the
metacharacters and metasequences listed in
. For expanded
definitions of each metacharacter, see
Table 1-10. Character representations
Alert (bell).
Backspace, x08
, supported only in character
ESC character, x1B
Newline, x0A
Carriage return, x0D
Form feed, x0C
Horizontal tab, x09
Character specified by a one-, two-, or three-digit octal
Character specified by a two-digit hexadecimal
Unicode character specified by a four-digit hexadecimal
Named control
character.
Table 1-11. Character classes and class-like
constructs
A single character listed or contained in a listed
A single character not listed and not contained within a listed
Any character, except a line terminator (unless DOTALL
Word character, [a-zA-Z0-9_]
Non-word character, [^a-zA-Z0-9_]
Digit, [0-9]
Non-digit, [^0-9]
Whitespace character, [ \t\n\f\r\x0B]
Non-whitespace character, [^
\t\n\f\r\x0B]
Character contained by given POSIX character class, Unicode
property, or Unicode block.
Character not contained by given POSIX character class, Unicode
property, or Unicode block.
Table 1-12. Anchors and other zero-width
Start of string, or after any newline if in MULTILINE
Beginning of string, in any match mode.
End of string, or before any newline if in MULTILINE
End of string but before any final line terminator, in any
match mode.
End of string, in any match mode.
Word boundary.
Not-word-boundary.
Beginning of current search.
Positive lookahead.
Negative lookahead.
Positive lookbehind.
lookbehind.
Table 1-13. Comments and mode modifiers
Modifier/sequence
Mode character
Pattern.UNIX_LINES
as the only line terminator.
Pattern.DOTALL
Dot (.) matches any character, including a line
terminator.
Pattern.MULTILINE
match next to embedded line
terminators.
Ignore whitespace and allow embedded comments starting with
Pattern.CASE_INSENSITIVE
Case-insensitive match for ASCII characters.
Pattern.UNICODE_CASE
Case-insensitive match for Unicode characters.
Pattern.CANON_EQ
Unicode "canonical equivalence" mode where characters or
sequences of a base character and combining characters with identical visual
representations are treated as equals.
Turn listed modes (idmsux
) on for the rest of the
subexpression.
Turn listed modes (idmsux
) off for the rest of the
subexpression.
Turn listed modes (idmsux
) on within
parentheses.
Turn listed modes (idmsux
) off within
parentheses.
Treat rest of line as a comment in /x
Table 1-14. Grouping, capturing, conditional, and
Group subpattern and capture submatch into
,... and $1
Contains text matched by the n
th capture
In a replacement string, contains text matched by the
th capture group.
Groups subpattern, but does not capture submatch.
Disallow backtracking for text matched by
subpattern.
Try subpatterns in alternation.
Match 0 or more times.
Match 1 or more times.
Match 1 or 0 times.
Match exactly n
Match at least n
Match at least x
times, but no more than
Match 0 or more times, but as few times as
Match 1 or more times, but as few times as
Match 0 or 1 times, but as few times as possible.
Match at least n
times, but as few times as
Match at least x
times, no more than
times, and as few times as possible.
Match 0 or more times, and never backtrack.
Match 1 or more times, and never backtrack.
Match 0 or 1 times, and never backtrack.
Match at least n
times, and never
backtrack.
Match at least n
times, and never
backtrack.
Match at least x
times, no more than
times, and never
backtrack.
1.4.2 Regular Expression Classes and Interfaces
introduces two main classes, java.util.regex.Pattern
and java.util.regex.Matcher
; an exception,
java.util.regex.PatternSyntaxException
; and a new interface, CharSequence
. Additionally, Sun upgraded the
class to implement the CharSequence
interface and to
provide basic pattern-matching methods. Pattern
objects are compiled
regular expressions that can be applied to many strings. A Matcher
object is a match of one Pattern
applied to one string (or any object
implementing CharSequence
Backslashes in regular expression
literals need to be escaped. So \n
(newline) becomes
when used in a Java String
literal that is to be used as a
regular expression.
java.lang.String
Description
New methods for pattern matching.
boolean matches (String
Return true if regex
matches the entire
String[ ] split (String
Return an array of the substrings surrounding matches of
String [ ] split (String
Return an array of the substrings surrounding the first
matches of regex
String replaceFirst (String
replacement
Replace the substring matched by regex
replacement
String replaceAll (String
replacement
Replace all substrings matched by regex
replacement
java.util.regex.Pattern
extends Object and implements
Serializable
Description
Models a regular expression pattern.
static Pattern compile(String
Construct a Pattern
object from
static Pattern compile(String
Construct a new Pattern
object out of
and the OR'd mode-modifier constants
Return the Pattern
's mode modifiers.
Matcher matcher(CharSequence
Construct a Matcher
object that will match this
against input
static boolean matches(String
CharSequence
Return true if regex
matches the entire string
String pattern( )
Return the regular expression used to create this
String[ ] split(CharSequence
Return an array of the substrings surrounding matches of this
String[ ] split(CharSequence
Return an array of the substrings surrounding the first
matches of this pattern in
java.util.regex.Matcher
extends Object
Description
Models a regular expression pattern matcher and pattern
matching results.
Matcher appendReplacement(StringBuffer
replacement
Append substring preceding match and
replacement
StringBuffer appendTail(StringBuffer
Appends substring following end of match to
Index of the first character after the end of the match.
Index of the first character after the text captured by
boolean find( )
Find the next match in the input string.
boolean find(int
Find the next match after character position,
String group( )
Text matched by this Pattern
String group(int
Text captured by capture group, group
groupCount( )
Number of capturing groups in Pattern
boolean lookingAt( )
True if match is at beginning of input.
boolean matches( )
Return true if Pattern
matches entire input
Pattern pattern( )
Return Pattern
object used by this
String replaceAll(String
replacement
Replace every match with replacement
String replaceFirst(String
replacement
Replace first match with replacement
Matcher reset( )
Reset this matcher so that the next match starts at the
beginning of the input string.
Matcher reset(CharSequence
Reset this matcher with new input
Index of first character matched.
Index of first character matched in captured substring,
java.util.regex.PatternSyntaxException
implements Serializable
Description
Thrown to indicate a syntax error in a regular expression
PatternSyntaxException(String
Construct an instance of this class.
String getDescription( )
Return error description.
getIndex( )
Return error index.
String getMessage( )
Return a multiline error message containing error description,
index, regular expression pattern, and indication of the position of the error
within the pattern.
String getPattern( )
Return the regular expression pattern that threw the
exception.
java.lang.CharSequence
implemented by CharBuffer, String,
StringBuffer
Description
Defines an interface for read-only access so that regular
expression patterns may be applied to a sequence of characters.
charAt(int
Return the character at the zero-based position,
Return the number of characters in the sequence.
CharSequence subSequence(int
Return a subsequence including the start
and excluding the end
String toString( )
Return a String
representation of the
1.4.3 Unicode Support
This package supports Unicode 3.0, although
support only ASCII. You can use the equivalent Unicode properties
, and \P{Z}
. The word boundary sequences, \b
, do understand Unicode.
For supported Unicode properties and blocks, see
. This package supports only the short property names, such as
, and not \p{Lowercase_Letter}
. Block names require the
prefix and support only the name form without s
for example, \p{InGreekExtended}
, not \p{In_Greek_Extended}
\p{In Greek Extended}
1.4.4 Examples
Example 1-5. Simple
//Match Spider-Man, Spiderman, SPIDER-MAN, etc.
public class StringRegexTest {
public static void main(String[
] args) throws Exception {
String dailybugle = "Spider-Man Menaces City!";
//regex must match entire string
String regex = "(?i).*spider[- ]?man.*";
if (dailybugle.matches(regex)) {
//do something
Example 1-6. Match and
capture group
//Match dates formatted like MM/DD/YYYY, MM-DD-YY,...
import java.util.regex.*;
public class MatchTest {
public static void main(String[
] args) throws Exception {
String date = "12/30/1969";
Pattern p =
pile("(\\d\\d)[-/](\\d\\d)[-/](\\d\\d(?:\\d\\d)?)");
Matcher m = p.matcher(date);
if (m.find(
String month = m.group(1);
String day
= m.group(2);
String year
= m.group(3);
Example 1-7. Simple
substitution
//Convert &br& to &br /& for XHTML compliance
import java.util.regex.*;
public class SimpleSubstitutionTest {
public static void main(String[
String text = "Hello world. &br&";
Pattern p = pile("&br&", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);
String result = m.replaceAll("&br /&");
catch (PatternSyntaxException e) {
System.out.println(e.getMessage(
catch (Exception e) { System.exit(
Example 1-8. Harder
substitution
//urlify - turn URL's into HTML links
import java.util.regex.*;
public class Urlify {
public static void main (String[
] args) throws Exception {
String text = "Check the website, /catalog/repr.";
String regex =
# start at word\n"
# boundary\n"
# capture to $1\n"
"(https?|telnet|gopher|file|wais|ftp) : \n"
# resource and colon\n"
"[\\w/\\#~:.?+=&%@!\\-] +?
# one or more valid\n"
# characters\n"
# but take as little\n"
# as possible\n"
# lookahead\n"
"[.:?\\-] *
# for possible punc\n"
"(?: [^\\w/\\#~:.?+=&%@!\\-] # invalid character\n"
# or end of string\n"
Pattern p = pile(regex,
Pattern.CASE_INSENSITIVE + MENTS);
Matcher m = p.matcher(text);
String result = m.replaceAll("&a href=\"$1\"&$1&/a&");
1.4.5 Other Resources
, by Ron Hitchens
(O'Reilly), shows regular expressions in the context of Java's new I/O
improvements.
Mastering Regular Expressions
Second Edition, by Jeffrey E. F. Friedl (O'Reilly), covers the details of Java
regular expressions on pages 378-391.
Sun's online documentation at
浏览: 365671 次
来自: 杭州
不好意思,请问这确定是回调机制吗。你的例子中只是将接口的实现类 ...
写的真乱啊,完全不知所云...
junia_1 写道
shock: ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'java.util.regex.PatternSyntaxException&Dangling&meta&character&'*'&near&index&0
String s = "The^rain^in*Spain*falls*mainly in the
String[] ss = s.split("*");
结果报错:java.util.regex.PatternSyntaxException: Dangling meta
character '*' near index 0
+、*、|、\、?、^等符号在正则表达示中有相应的不同意义。
一般来讲只需要加[]、或是\\即可
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 java.util.regex jar 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信