匹配 java 标点符号:使用正则表达式 [\p{punct}]。[\p{punct}] 表示 unicode 标点符号类,匹配任何 unicode 标点符号字符。
Java 正则表达式匹配标点符号
如何使用 Java 正则表达式匹配标点符号?
Java 提供了 Pattern 和 Matcher 类,可以方便地使用正则表达式进行字符串操作。其中,用来匹配标点符号的正则表达式为:
[\p{Punct}]登录后复制
详细解释:
立即学习“Java免费学习笔记(深入)”;
\p{Punct} 表示 Unicode 标点符号类,它匹配任何 Unicode 标点符号字符。
方括号 [] 表示字符类,即匹配方括号内任何字符。
反斜杠 \ 转义了方括号内的字符,使其被解释为普通字符,而不是正则表达式元字符。
示例代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PunctuationMatcher {
public static void main(String[] args) {
String text = "This sentence contains various types of punctuation, such as commas(,), periods(.), and question marks(?)";
String regex = "[\\p{Punct}]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println("Found punctuation mark: " + matcher.group());
}
}
}登录后复制
输出:
Found punctuation mark: ,
Found punctuation mark: (
Found punctuation mark: )
Found punctuation mark: .
Found punctuation mark: ?登录后复制以上就是java正则表达式匹配标点符号的详细内容,更多请关注php中文网其它相关文章!


JosephJaf13 天前
发表在:MagicEXIF通用注册机 v1.13充满正能量的 旅行分享! 感谢激励。 <...
JosephJaf21 天前
发表在:Intel XTU中文补丁 1.13欣赏你的照片, 我感受到, 旅游让人相连...
JosephJaf22 天前
发表在:MagicEXIF通用注册机 v1.13我热爱这样的想法, 那么放松地度假。真的...
BrianSab1 个月前
发表在:南通速强批量添加水印专家 v1.83Эта статья для ознак...
Ronaldgag1 个月前
发表在:11日17日,星期一,在这里每天60秒读懂世界!Free PHP Blockchain ...
NelsonBOT1 个月前
发表在:11日16日,星期日,在这里每天60秒读懂世界!Free non-criminal in...
Darrenjhjhjhcunny1 个月前
发表在:11日14日,星期五,在这里每天60秒读懂世界!Атака черной материи...
parifoot-rdc-791 个月前
发表在:laravel 找不到页面Votre guide <a href=...
Anya142Sa2 个月前
发表在:ASUS华硕A8N-SLI Deluxe主板BIOS 10110Hello friends! I c...
91资源网站长-冰晨2 个月前
发表在:广告合作123