匹配一个“."在 Java 中 [英] Matching a "." in java

查看:40
本文介绍了匹配一个“."在 Java 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串

String srcString = "String1.String2.String3";

我想在."上拆分srcString"

I want to split "srcString" on "."

使用 srcString.split(".") 匹配所有字符.

Using srcString.split(".") is matching all the characters.

匹配."的正则表达式是什么??

What is the regex to match a "." ?

推荐答案

在正则表达式中 dot特殊字符,表示任何字符,除了行分隔符(为了匹配行分隔符,使用 Pattern.DOTALL 标志).

In regex dot is special character representing any character except line separator (to also make it match line separators use Pattern.DOTALL flag).

无论如何,使用split("\\.")

说明:

  • 转义 . 我们可以在它之前添加 \ 所以我们最终得到正则表达式 \.
  • 现在因为 \ 在字符串文字中也是特殊的 "" 我们也需要在那里转义它,所以为了表达 \. 我们需要把它写成 "\\.".
  • to escape . we can add \ before it so we end up with regex \.
  • now since \ is also special in string literal " " we also need to escape it there, so to express \. we need to write it as "\\.".

这篇关于匹配一个“."在 Java 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆