[英]regex to find substring between special characters
I am running into this problem in Java. 我在Java中遇到了这个问题。
I have data strings that contain entities enclosed between &
and ;
我的数据字符串包含
&
和;
之间的实体;
For eg 例如
&Text.ABC;, &Links.InsertSomething;
These entities can be anything from the ini file we have. 这些实体可以是我们拥有的ini文件中的任何内容。
I need to find these string in the input string and remove them. 我需要在输入字符串中找到这些字符串并将其删除。 There can be none, one or more occurrences of these entities in the input string.
在输入字符串中可能没有这些实体,也可能没有一个或多个。
I am trying to use regex to pattern match and failing. 我正在尝试使用正则表达式来进行模式匹配和失败。
Can anyone suggest the regex for this problem? 谁能建议这个问题的正则表达式?
Thanks! 谢谢!
Here is the regex: 这是正则表达式:
"&[A-Za-z]+(\\.[A-Za-z]+)*;"
It starts by matching the character &
, followed by one or more letters (both uppercase and lower case) ( [A-Za-z]+
). 它以匹配字符
&
开头,后跟一个或多个字母(大写和小写)( [A-Za-z]+
)。 Then it matches a dot followed by one or more letters ( \\\\.[A-Za-z]+
). 然后,它匹配一个点,后跟一个或多个字母(
\\\\.[A-Za-z]+
)。 There can be any number of this, including zero. 可以有任何数量,包括零。 Finally, it matches the
;
最后,它匹配
;
character. 字符。
You can use this regex in java like this: 您可以像这样在Java中使用此正则表达式:
Pattern p = Pattern.compile("&[A-Za-z]+(\\.[A-Za-z]+)*;"); // java.util.regex.Pattern
String subject = "foo &Bar; baz\n";
String result = p.matcher(subject).replaceAll("");
Or just 要不就
"foo &Bar; baz\n".replaceAll("&[A-Za-z]+(\\.[A-Za-z]+)*;", "");
If you want to remove whitespaces after the matched tokens, you can use this re: 如果要在匹配的标记后删除空格,可以使用以下命令:
"&[A-Za-z]+(\\.[A-Za-z]+)*;\\s*" // the "\\s*" matches any number of whitespace
And there is a nice online regular expression tester which uses the java regexp library. 还有一个不错的在线正则表达式测试器,它使用java regexp库。
http://www.regexplanet.com/simple/index.html http://www.regexplanet.com/simple/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.