我想打印包含模式_fact
的单词。例如,在下面的字符串中要打印agg_day_adv_campaign_demand_fact
。
String text =
"FROM dt_dim AS d, "+
"adj_dim AS adj " +
"cam_dim AS cam, " +
"agg_day_adv_campaign_demand_fact AS fact ";
答案 0 :(得分:3)
regex \ w * _fact会做到这一点。 您可以在此处测试此正则表达式(http://regexpal.com/)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String text =
"FROM dt_dim AS d, "+
"adj_dim AS adj " +
"cam_dim AS cam, " +
"agg_day_adv_campaign_demand_fact AS fact ";
Pattern pattern = Pattern.compile("\\w*_fact");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println("Found the text " + matcher.group()+ "starting at " + matcher.start()
+ " index and ending at index " + matcher.end());
System.out.println(text.substring(matcher.start(), matcher.end()));
}
}
}
答案 1 :(得分:0)
您可以简单地将split
字符串转换为单词并逐个检查单词以搜索您的模式。
答案 2 :(得分:0)
String text =
"FROM dt_dim AS d, "+
"adj_dim AS adj " +
"cam_dim AS cam, " +
"agg_day_adv_campaign_demand_fact AS fact ";
Pattern FACT_PAT = Pattern.compile("(\\S+_fact)");
Matcher m = FACT_PAT.matcher(text);
if (m.find()){
System.out.println(m.group(1));
}