在Java中,如何从字符串中获取多个子字符串加上额外的数字?
假设我们有以下字符串:在Java中,如何从字符串中获取多个子字符串加上额外的数字?,java,regex,Java,Regex,假设我们有以下字符串: (form_component_34=="Yes"||case_attribute_37==3&&case_attribute_40?has_content) 我只想得到它的操作数: 表格第34部分 案例属性37 案例属性40 它们总是以字符串“form_component_uu”或“case_attribute_uu”开头,后面有一个数字(作为ID)。我想我应该使用regexp 你们谁能帮我一下吗?你们可以使用以下正则表达式: (?:form_co
(form_component_34=="Yes"||case_attribute_37==3&&case_attribute_40?has_content)
我只想得到它的操作数:
- 表格第34部分
- 案例属性37
- 案例属性40
你们谁能帮我一下吗?你们可以使用以下正则表达式:
(?:form_component_|case_attribute_)\\d+
Java代码:
String str = "(form_component_34==\"Yes\"||case_attribute_37==3&&case_attribute_40?has_content)";
Pattern r = Pattern.compile("(?:form_component_|case_attribute_)\\d+");
ArrayList<String> matches = new ArrayList<String>();
Matcher m = r.matcher(str);
while (m.find())
{
matches.add(m.group());
}
System.out.println(matches);
请参见以下代码
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternMatching
{
public static void main( String args[] ){
// String to be scanned to find the pattern.
String line = "(form_component_34==\"Yes\"||case_attribute_37==3&&case_attribute_40?has_content)";
String pattern = "(?:form_component_|case_attribute_)\\d+";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
while(m.find()) {
System.out.println(""+m.group());
}
}
}
所以你想要像
34
,37
之类的值吗?如果我不清楚,很抱歉,我想得到完整的组件名称和数字,比如“form_component_34”。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternMatching
{
public static void main( String args[] ){
// String to be scanned to find the pattern.
String line = "(form_component_34==\"Yes\"||case_attribute_37==3&&case_attribute_40?has_content)";
String pattern = "(?:form_component_|case_attribute_)\\d+";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
while(m.find()) {
System.out.println(""+m.group());
}
}
}