[英]Extracting variables from mathematical equation
我有一个像
a +(b * 6)<=猫* 45 &&猫=狗
我试图提取变量a, b, cat, dog
。 下面是我的代码。
Set<String> varList = null;
StringBuilder sb = null;
String expression = "a+(b * 6) <= cat*45 && cat = dog";
if (expression!=null)
{
sb = new StringBuilder();
//list that will contain encountered words,numbers, and white space
varList = new HashSet<String>();
Pattern p = Pattern.compile("[A-Za-z\\s]");
Matcher m = p.matcher(expression);
//while matches are found
while (m.find())
{
//add words/variables found in the expression
sb.append(m.group());
}//end while
//split the expression based on white space
String [] splitExpression = sb.toString().split("\\s");
for (int i=0; i<splitExpression.length; i++)
{
varList.add(splitExpression[i]);
}
}
Iterator iter = varList.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
我得到的输出是:
ab
cat
dog
要求的输出:
a
b
cat
dog
在这种情况下,变量可能会或可能不会由空格分隔。 有空格时,输出良好。 但是如果变量没有用空格隔开,则输出错误。 有人可以建议我正确的Pattern
吗?
为什么要使用正则表达式find()
循环提取单词,然后将它们全部串联成一个字符串以再次拆分该字符串?
只需使用正则表达式找到的单词即可。
好吧,就是说,从表达式中删除空格( \\\\s
)并使其与整个单词( +
)匹配之后,当然。
Pattern p = Pattern.compile("[A-Za-z]+");
Matcher m = p.matcher(expression);
while (m.find())
{
varList.add(m.group());
}
此正则表达式应该有效( variable name can start with uppercase or lowercase and can then contain digit(s), underscore, uppercase and lowercase
)
\b[A-Za-z]\w*\b
Java代码
Set<String> set = new HashSet<String>();
String line = "a+(b * 6) <= cat*45 && cat = dog";
String pattern = "\\b([A-Za-z]\\w*)\\b";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while (m.find()) {
set.add(m.group());
}
System.out.println(set);
我相信您应该用“ [A-Za-z] +”代替您的正则表达式。 我只是用Python模拟
>>> re.findall('[A-Za-z]+', 'a+(b * 6) <= cat*45 && cat = dog')
['a', 'b', 'cat', 'cat', 'dog']
>>>
接下来,将结果列表放入集合中:
>>> rs = set(re.findall('[A-Za-z]+', 'a+(b * 6) <= cat*45 && cat = dog'))
>>> for w in rs:
... print w,
...
a b dog cat
>>>
完整的工作代码
public static void main(String[] args) {
Set<String> varList = null;
StringBuilder sb = null;
String expression = "a+(b * 6) <= cat*45 && cat = dog";
if (expression!=null)
{
sb = new StringBuilder();
//list that will contain encountered words,numbers, and white space
varList = new HashSet<String>();
Pattern p = Pattern.compile("[A-Za-z\\s]+");
Matcher m = p.matcher(expression);
//while matches are found
while (m.find())
{
//add words/variables found in the expression
sb.append(m.group());
sb.append(",");
}//end while
//split the expression based on white space
String [] splitExpression = sb.toString().split(",");
for (int i=0; i<splitExpression.length; i++)
{
if(!splitExpression[i].isEmpty() && !splitExpression[i].equals(" "))
varList.add(splitExpression[i].trim());
}
}
Iterator iter = varList.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.