[英]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.