java正则表达式找到特定格式语句的关键字
java正则表达式找出特定格式语句的关键字
中文问句语句分析需要提取问句中的关键字
例子1
我们需要提取上面语句的"牌子"和"价格"这两个关键字
用正则实现
按上面的实现
完全可以实现 什么|甚么|啥 ***的|啊|呢?这种语句的关键字提取
例子2
现在要提取多个关键字
我们要提取“红色”“蓝色”和 “水货”,“正品”这些关键字
中文问句语句分析需要提取问句中的关键字
例子1
这个是什么牌子的? 这个是啥价格?
我们需要提取上面语句的"牌子"和"价格"这两个关键字
用正则实现
String reg="((?<=(什么|啥))[^的?\\?]+)"; Pattern pattern =Pattern.compile(reg); Matcher m=pattern.matcher("这个是什么牌子的?"); while(m.find()){ System.out.println(m.group());//输出“牌子” } m=pattern.matcher("这个是啥价格?"); while(m.find()){ System.out.println(m.group());//输出“价格” }
按上面的实现
完全可以实现 什么|甚么|啥 ***的|啊|呢?这种语句的关键字提取
例子2
现在要提取多个关键字
这种产品是红色的还是蓝色的啊? 这种产品是水货还是正品呢?
我们要提取“红色”“蓝色”和 “水货”,“正品”这些关键字
String reg="((?<=(是))[^的还呢]+)"; Pattern pattern =Pattern.compile(reg); Matcher m=pattern.matcher("这种产品是红色的还是蓝色的啊?"); while(m.find()){ System.out.println(m.group());//输出“红色”“蓝色” } m=pattern.matcher("这种产品是水货还是正品呢?"); while(m.find()){ System.out.println(m.group());//输出“水货”“正品” }