Java Regex备忘单

这篇文章将为我自己提供更多注释。 我本不想写一篇文章,但找不到我想要的综合文章。 因此,这篇文章将不是一个很好的文章,而且由于有数百万个出色的资源,我一般不会谈论表达。

与JavaScript相比,在Java中使用Regex有点烦人。 是的,的确如此,这就是我偶然写这篇文章的原因。

Syntax

对于模式,而不是单个\,Java需要双反斜杠\\。 好的方面是,我对其他编辑器一无所知,但至少在NetBeans中,当您从剪贴板粘贴代码时,额外的\ is automatically added。 此外,不同的字符串需要它自己的匹配器除非用火柴() method。

Methods

Pattern
Pattern compile(String regex[, int flags])    // flags optional, use fields sepreate by |
boolean matches([String regex, ]CharSequence input)    // regex optional.
String[] split(String regex[, int limit])    // limit optional.
String quote(String s)    // returns a literal pattern String for the specified String.
Matcher
// for names, in regex, (?<name>pattern)
int start([int group | String name])    // argument optional
int end([int group | String name])    // argument optional.
boolean find([int start])    // start is optional.
String group([int group | String name])    // argument optional.
Matcher reset()
String
boolean matches(String regex)
String replaceAll(String regex, String replacement)
String[] split(String regex[, int limit])    // limit optional

有更多的方法。

Usages

If there is a match exist.
String str = "She sells seashells by the seashore";
String reg = "\\w*se\\w*";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
System.out.println(m.find());    // true
If they are identical
String str = "She sells seashells by the seashore";
String reg = "\\w*se\\w*";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
System.out.println(m.matches());    // false, only true when
                                    // the pattern matches with string
                                    // without remainder.

System.out.println("seashore".matches(reg));    // true

要么,

String str = "She sells seashells by the seashore";
String reg = "\\w*se\\w*";
System.out.println(Pattern.matches(reg, str);    // false
Array of all matches
String str = "She sells seashells by the seashore";
String reg = "\\w*se\\w*";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
List<String> matches = new ArrayList<>();
while (m.find()) {
    matches.add(m.group());
}
System.out.println(matches);    // [sells, seashells, seashore]
Count number of matches
String str = "She sells seashells by the seashore";
String reg = "\\w*se\\w*";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
int counter = 0;
while (m.find()) {
    counter++;
}

我发现代码高尔夫并不能在所有情况下都很好。 匹配项数组相同。

Flags

所有标志都是Pattern类的字段。 在Java中,您可能会认为所有Regex都是全局的。

爪哇爪哇scr一世pt eqü一世valent说明佳能启用规范对等。不区分大小写一世Enables case-一世nsens一世t一世ve 米atch一世ng.注释wh一世te space and co米米ents are 一世gnored 一世n the pattern.点球sdot 米atches end l一世ne character as well.文字Enables l一世teral pars一世ng of the pattern.多线米Enables 米ült一世l一世ne 米ode.UNICODE_CASEüEnables Un一世code-aware case fold一世ng.UNIX_LINESonly the '\n' l一世ne ter米一世nator 一世s recogn一世zed 一世n the behav一世oür of ., ,和$。

可能有我错过的等效Javascript。

Reference

from: https://dev.to//voidjuneau/java-regex-cheat-sheet-ob0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值