Java中用于分割复数字符串的正则表达式
在Java中寻找正则表达式来分隔表示复数的字符串。一个代码示例就好了。 输入字符串的格式为:Java中用于分割复数字符串的正则表达式,java,regex,Java,Regex,在Java中寻找正则表达式来分隔表示复数的字符串。一个代码示例就好了。 输入字符串的格式为: "a+bi" Example: "300+400i", "4+2i", "5000+324i". 我需要从字符串中检索300和400。” 我知道我们可以用这种方法粗略地做 str.substring(0, str.indexOf('+')); str.substring(str.indexOf('+')+1,str.indexOf("i")); 我需要从字符串中检索300和400 使用String.
"a+bi"
Example: "300+400i", "4+2i", "5000+324i".
我需要从字符串中检索300和400。”
我知道我们可以用这种方法粗略地做
str.substring(0, str.indexOf('+'));
str.substring(str.indexOf('+')+1,str.indexOf("i"));
我需要从字符串中检索300和400
使用String.split(regex)
函数怎么样:
String s[] = "300-400i".split("[\\Q+-\\Ei]");
System.out.println(s[0]+" "+s[1]); //prints 300 400
我需要从字符串中检索300和400
使用String.split(regex)
函数怎么样:
String s[] = "300-400i".split("[\\Q+-\\Ei]");
System.out.println(s[0]+" "+s[1]); //prints 300 400
使用这个:
[0-9]{1,}
它会返回数字
希望能有所帮助。使用这个:
[0-9]{1,}
它会返回数字
希望有帮助。与此匹配的正则表达式是:
/[0-9]{1,}[+-][0-9]{1,}i/
您可以使用以下方法:
Pattern complexNumberPattern = Pattern.compile("[0-9]{1,}");
Matcher complexNumberMatcher = complexNumberPattern.matcher(myString);
并使用
complexNumberMatcher
上的find
和group
方法从myString
中检索与此匹配的正则表达式:/[0-9]{1,}[+-][0-9]{1,}i/
您可以使用以下方法:
Pattern complexNumberPattern = Pattern.compile("[0-9]{1,}");
Matcher complexNumberMatcher = complexNumberPattern.matcher(myString);
然后使用
complexNumberMatcher
上的find
和group
方法从myString
检索数字,试试这个。对我来说,它是有效的
public static void main(String[] args) {
String[] attempts = new String[]{"300+400i", "4i+2", "5000-324i", "555", "2i", "+400", "-i"};
for (String s : attempts) {
System.out.println("Parsing\t" + s);
printComplex(s);
}
}
static void printComplex(String in) {
String[] parts = in.split("[+-]");
int re = 0, im = 0, pos = -1;
for (String s : parts) {
if (pos != -1) {
s = in.charAt(pos) + s;
} else {
pos = 0;
if ("".equals(s)) {
continue;
}
}
pos += s.length();
if (s.lastIndexOf('i') == -1) {
if (!"+".equals(s) && !"-".equals(s)) {
re += Integer.parseInt(s);
}
} else {
s = s.replace("i", "");
if ("+".equals(s)) {
im++;
} else if ("-".equals(s)) {
im--;
} else {
im += Integer.parseInt(s);
}
}
}
System.out.println("Re:\t" + re + "\nIm:\t" + im);
}
输出:
Parsing 300+400i
Re: 300
Im: 400
Parsing 4i+2
Re: 2
Im: 4
Parsing 5000-324i
Re: 5000
Im: -324
Parsing 555
Re: 555
Im: 0
Parsing 2i
Re: 0
Im: 2
试试这个。对我来说,它是有效的
public static void main(String[] args) {
String[] attempts = new String[]{"300+400i", "4i+2", "5000-324i", "555", "2i", "+400", "-i"};
for (String s : attempts) {
System.out.println("Parsing\t" + s);
printComplex(s);
}
}
static void printComplex(String in) {
String[] parts = in.split("[+-]");
int re = 0, im = 0, pos = -1;
for (String s : parts) {
if (pos != -1) {
s = in.charAt(pos) + s;
} else {
pos = 0;
if ("".equals(s)) {
continue;
}
}
pos += s.length();
if (s.lastIndexOf('i') == -1) {
if (!"+".equals(s) && !"-".equals(s)) {
re += Integer.parseInt(s);
}
} else {
s = s.replace("i", "");
if ("+".equals(s)) {
im++;
} else if ("-".equals(s)) {
im--;
} else {
im += Integer.parseInt(s);
}
}
}
System.out.println("Re:\t" + re + "\nIm:\t" + im);
}
输出:
Parsing 300+400i
Re: 300
Im: 400
Parsing 4i+2
Re: 2
Im: 4
Parsing 5000-324i
Re: 5000
Im: -324
Parsing 555
Re: 555
Im: 0
Parsing 2i
Re: 0
Im: 2
正则表达式
示例正则表达式
http://rubular.com/r/FfOAt1zk0v
示例Java
string regexPattern =
// Match any float, negative or positive, group it
@"([-+]?\d+\.?\d*|[-+]?\d*\.?\d+)" +
// ... possibly following that with whitespace
@"\s*" +
// ... followed by a plus
@"\+" +
// and possibly more whitespace:
@"\s*" +
// Match any other float, and save it
@"([-+]?\d+\.?\d*|[-+]?\d*\.?\d+)" +
// ... followed by 'i'
@"i";
Regex regex = new Regex(regexPattern);
Console.WriteLine("Regex used: " + regex);
while (true)
{
Console.WriteLine("Write a number: ");
string imgNumber = Console.ReadLine();
Match match = regex.Match(imgNumber);
double real = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
double img = double.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
Console.WriteLine("RealPart={0};Imaginary part={1}", real, img);
}
正则表达式
示例正则表达式
http://rubular.com/r/FfOAt1zk0v
示例Java
string regexPattern =
// Match any float, negative or positive, group it
@"([-+]?\d+\.?\d*|[-+]?\d*\.?\d+)" +
// ... possibly following that with whitespace
@"\s*" +
// ... followed by a plus
@"\+" +
// and possibly more whitespace:
@"\s*" +
// Match any other float, and save it
@"([-+]?\d+\.?\d*|[-+]?\d*\.?\d+)" +
// ... followed by 'i'
@"i";
Regex regex = new Regex(regexPattern);
Console.WriteLine("Regex used: " + regex);
while (true)
{
Console.WriteLine("Write a number: ");
string imgNumber = Console.ReadLine();
Match match = regex.Match(imgNumber);
double real = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
double img = double.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
Console.WriteLine("RealPart={0};Imaginary part={1}", real, img);
}
理论上,你可以用这样的方法:
Pattern complexNumberPattern = Pattern.compile("(.*)+(.*)");
Matcher complexNumberMatcher = complexNumberPattern.matcher(myString);
if (complexNumberMatcher.matches()) {
String prePlus = complexNumberMatcher.group(1);
String postPlus = complexNumberMatcher.group(2);
}
与选择数字相比,这样做的优势在于,您可以阅读以下内容:
5b+17c作为5b和17c
编辑:只是注意到你不想要这些字母,所以没关系,但这会让你在其他字母出现的情况下对其进行更多的控制。理论上,你可以使用这样的方式:
Pattern complexNumberPattern = Pattern.compile("(.*)+(.*)");
Matcher complexNumberMatcher = complexNumberPattern.matcher(myString);
if (complexNumberMatcher.matches()) {
String prePlus = complexNumberMatcher.group(1);
String postPlus = complexNumberMatcher.group(2);
}
与选择数字相比,这样做的优势在于,您可以阅读以下内容:
5b+17c作为5b和17c
编辑:只是注意到你不想要这些信,所以没关系,但是这会给你更多的控制权,以防其他字母出现在其中。为什么你更喜欢正则表达式而不是那几行非常简洁的代码?只是想看看它是否可能,语法:)为什么你更喜欢正则表达式而不是那几行非常简洁的代码?只是想看看它是否可能,语法:)嘿,MJafar,你的代码对我有用,但我选择Sage的答案是因为简单和缺乏输入。但是我喜欢这种方法:)我也喜欢他的答案,非常好的方法,但是记住我的代码,把它保存在某个地方,这是一种在更大的字符串中获得与特定模式匹配的子字符串的方法。当输入有额外的空格时,这种方法也有效,如
300+400i
。Sage的答案可以用trim()
方法更正:嘿,MJafar,你的代码对我很有用,但我选择Sage的答案是因为简单和缺乏导入。但是我喜欢这种方法:)我也喜欢他的答案,非常好的方法,但是记住我的代码,把它保存在某个地方,这是一种在更大的字符串中获得与特定模式匹配的子字符串的方法。当输入有额外的空格时,这种方法也有效,如300+400i
。Sage的答案可以用trim()
方法更正:你忘了在第二个字符串中包含a-bi
。不包括-的可能性。明白了,我的错。没有刷新:)@AshokFelix,没关系。谢谢:)你忘了在第二个字符串中包含a-bi
。不包括-的可能性。明白了,我的错。没有刷新:)@AshokFelix,没关系。谢谢:)java.lang.NumberFormatException:对于输入字符串:“+400”我将其作为示例发布。一些if语句解决了这个问题。请参阅updatejava.lang.NumberFormatException:以获取输入字符串:“+400”作为示例。一些if语句解决了这个问题。请参阅更新可以修改为句柄-对于虚部?它目前只处理实际零件的负值。如果第一部分给出了虚部,是否可以修改为处理虚部?它目前只处理实际零件的负值。如果第一部分给出了虚部?