简体   繁体   English

Java Regex在字符串中查找“ [”的实例

[英]Java Regex to find instances of “[” in a string

I'm currently working on a UIMA based project, and the data set I am using has some predefined plain-text annotations that I am trying to convert into UIMA annotations using the Matcher utility. 我目前正在基于UIMA的项目上工作,我使用的数据集具有一些预定义的纯文本注释,我正尝试使用Matcher实用程序将其转换为UIMA注释。

My problem is that the annotations are in the format [ANNO] [/ANNO], and I have no idea how to write '[' or ']' as a regular expression. 我的问题是注释的格式为[ANNO] [/ ANNO],我不知道如何将'['或']'编写为正则表达式。

I tried searching various places, and couldn't find an answer to this, closest I could find is to use either the octal or hexidecimal value rendition, but I then can't actually find said rendition for the character. 我尝试在各个地方搜索,但找不到答案,我能找到的最接近的是使用八进制或十六进制值表示法,但实际上我找不到该字符的表示形式。

Cheers 干杯

As Dervall said, you must escape them: 正如Dervall所说,您必须逃脱它们:

Pattern pattern = pattern.compile("\\[");
Matcher matcher = pattern.matcher("string to match against");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM