Java-提取两个特殊字符之间的字符串的一部分 [英] Java- Extract part of a string between two special characters
问题描述
我一直试图找出如何在两个特殊字符之间提取字符串的一部分'和'我一直在研究正则表达式,但坦率地说我无法理解它。
Java中的示例代码:
I have been trying to figure out how to extract a portion of a string between two special characters ' and " I've been looking into regex, but frankly I cannot understand it.
Example in Java code:
String str="21*90'89\"";
我想退出89
一般来说,我想知道如何在两个特定字符之间提取字符串的一部分。
In general I would just like to know how to extract part of a string between two specific characters please.
此外,如果知道如何提取部分字符串,那将会很高兴。从开头到特定字符的字符串,比如得到21。
Also it would be nice to know how to extract part of the string from the beginning to a specific character like to get 21.
推荐答案
试试这个正则表达式:
'(.*?)"
作为Java字符串文字,您必须按如下方式编写:
As a Java string literal you will have to write it as follows:
"'(.*?)\""
这是一个更完整的示例,演示了如何将此正则表达式与<$ c $一起使用c>匹配器:
Here is a more complete example demonstrating how to use this regular expression with a Matcher
:
Pattern pattern = Pattern.compile("'(.*?)\"");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
看到它在线工作: ideone
这篇关于Java-提取两个特殊字符之间的字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!