Java 如何通过特定模式从字符串中提取子字符串
我有一个字符串对象,比如Java 如何通过特定模式从字符串中提取子字符串,java,Java,我有一个字符串对象,比如DUMMY\u CONTENT\u DUMMY 之前或之后的部分实际上是胡言乱语 需要的是介于两个下划线之间的一个。 在java中,有没有一种方法可以将这些内容提取出来? 也许我必须编写一个regex?在这种情况下,您不需要regex String str = "DUMMY_CONTENT_DUMMY"; String content = str.split("_")[1]; 在这种情况下,您不需要正则表达式 String str = "DUMMY_CONTENT_DU
DUMMY\u CONTENT\u DUMMY
之前或之后的部分实际上是胡言乱语
需要的是介于两个下划线之间的一个。
在java中,有没有一种方法可以将这些内容提取出来?
也许我必须编写一个regex
?在这种情况下,您不需要regex
String str = "DUMMY_CONTENT_DUMMY";
String content = str.split("_")[1];
在这种情况下,您不需要正则表达式
String str = "DUMMY_CONTENT_DUMMY";
String content = str.split("_")[1];
在这里,中间部分包含中间部分,在本例中为“BB”
在这里,中间部分包含中间部分,在本例中为“BB” 如果使用正则表达式,则可以使用前面或后面带有字符的下划线作为先行和后视技术的线索。使用Friedl关于正则表达式的书,我以这段代码为例
/* example method from reference
* REF: Friedel, J. Mastering Regular Expressions. Ch8: Java. circa. p.371.
*/
public static void simpleRegexTest(){
String myText = "DUMMY_CONTENT_DUMMY";
String myRegex = "(?<=.*_)(.*)(?=_.*)";
// compile the regex into a pattern that can be used repeatedly
java.util.regex.Pattern p = java.util.regex.Pattern.compile(myRegex);
// prepare the pattern to be applied to a given string
java.util.regex.Matcher m = p.matcher(myText);
// apply the pattern to the current region and see if the a match is found
// if will return the first match only
// while will return matches until there are no more
while (m.find()){
// use the match result
// get the span of text matched with group()
// in this particular instance, we are interested in group 1
String matchedText = m.group(1);
// get the index of the start of the span with start()
int matchedFrom = m.start();
// get the index of the end of the span with end()
int matchedTo = m.end();
System.out.println("matched [" + matchedText + "] " +
" from " + matchedFrom +
" to " + matchedTo + " .");
}
/*参考中的示例方法
*参考:Friedel,J.掌握正则表达式。Ch8:Java。大约。p、 371。
*/
公共静态void simpleRegexTest(){
字符串myText=“DUMMY\u CONTENT\u DUMMY”;
String myRegex=“(?如果使用正则表达式,则可以使用字符前后的下划线作为前瞻和落后技术的线索
/* example method from reference
* REF: Friedel, J. Mastering Regular Expressions. Ch8: Java. circa. p.371.
*/
public static void simpleRegexTest(){
String myText = "DUMMY_CONTENT_DUMMY";
String myRegex = "(?<=.*_)(.*)(?=_.*)";
// compile the regex into a pattern that can be used repeatedly
java.util.regex.Pattern p = java.util.regex.Pattern.compile(myRegex);
// prepare the pattern to be applied to a given string
java.util.regex.Matcher m = p.matcher(myText);
// apply the pattern to the current region and see if the a match is found
// if will return the first match only
// while will return matches until there are no more
while (m.find()){
// use the match result
// get the span of text matched with group()
// in this particular instance, we are interested in group 1
String matchedText = m.group(1);
// get the index of the start of the span with start()
int matchedFrom = m.start();
// get the index of the end of the span with end()
int matchedTo = m.end();
System.out.println("matched [" + matchedText + "] " +
" from " + matchedFrom +
" to " + matchedTo + " .");
}
/*参考中的示例方法
*参考:Friedel,J.《掌握正则表达式》。Ch8:Java.circa.p.371。
*/
公共静态void simpleRegexTest(){
字符串myText=“DUMMY\u CONTENT\u DUMMY”;
String myRegex=“(?以下是使用RegEx的方法
String middlePart = yourString.replaceAll("[^_]*_([^_]*)_[^_]*", "$1");
下面是使用正则表达式的方法
String middlePart = yourString.replaceAll("[^_]*_([^_]*)_[^_]*", "$1");
String#split(“#”)
?在Java中有很多方法可以做到这一点,现在你的问题是非常开放的,在研究方面没有太多表现。因此不鼓励基于观点的问题或会引起大量讨论的问题。String#split(#”)
?在Java中有很多方法可以做到这一点,而现在你的问题是非常开放的,在研究方面没有太多表现。因此,不鼓励基于观点的问题或会引起大量讨论的问题。与你开玩笑的是,split将正则表达式模式作为论点:)(+1)跟你开玩笑,split以正则表达式模式作为参数:)(+1)