問:這是我的一個虛數的正則表達式:正則表達式:複數
[0-9]{0,}\d\.[0-9]{0,}\d[i]|[0-9]{0,}\d[i]
只需要一個純粹的複雜零件的虛數。
當我的解析器遇到,例如String im = "2i";
,它被轉換爲String z = "complex(0, 2)" //complex(re, im)
- 這樣的:
Matcher m = pattern.regex.matcher(exp); //String exp = "2i + 5"
if(m.find())
{
String key = m.group(); //String key = "2i"
int type = pattern.token;
if(type == COMPLEX)
{
key = key.replaceAll("i", ""); // key = "2"
exp = m.replaceFirst(""); // exp = "5"
exp = "complex(0," + key +")" + exp; //exp = "complex(0,2) + 5"
}
return exp;
}
我可以採用某種正則表達式做一次?例如,任何十進制數字後跟字母「i」將轉換爲另一個字符串 - complex(0, number)
?
允許j以及讓工程師保持高興。 – Bathsheba
我現在有更大的問題:P –