gpt4 book ai didi

java - 将连续的字符串数据拆分为所需的垂直输出

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:43 24 4
gpt4 key购买 nike

我正在使用脚本在 jsp 页面中显示输出。

我从数据库中得到如下输出:

  out.println(a) ; //prints output Jan-2019 Feb-2019 March-2019 April-2019

out.println(b) ; //prints output 100100200300

我正在尝试使用 html css 在 jsp 页面中打印输出,如下所示:

    Month           Price
Jan-2019 100
Feb-2019 100
March-2019 200
April-2019 300

我在谷歌中搜索了很多仍然没有找到任何解决方案也尝试了不同的正则表达式代码仍然没有解决。任何帮助将不胜感激。

最佳答案

这是代码。它有意变得更加“冗长”,以便于在您的数据提要中更多地使用正则表达式。

String a = "Jan-2019 Feb-2019 March-2019 April-2019";
String b = "100100200300";

Pattern P1 = Pattern.compile("(\\w{3,})-(\\d{4})");
Pattern P2 = Pattern.compile("[1-9]+0+");

Vector<Integer> years = new Vector<>();
Vector<String> months = new Vector<>();
Vector<Integer> prices = new Vector<>();

Matcher m = P1.matcher(a);
while (m.find())
{
months.add(m.group(1));
years.add(new Integer(m.group(2)));
}

m = P2.matcher(b);
while (m.find()) prices.add(new Integer(m.group()));

// Useful for debugging, to make sure these are "parallel arrays"
// Parallel Arrays are almost *always* useful when regular-expressions & parsing is "happening."
System.out.println( "years.size():\t" + years.size() + '\n' +
"months.size():\t" + months.size() + '\n' +
"prices.size():\t" + prices.size() + "\n\n" );

int len = years.size();
for (int i=0; i < len; i++)
System.out.println( months.elementAt(i) + " " +
years.elementAt(i) + ":\t" +
prices.elementAt(i) );

System.exit(0);

这是输出:

years.size():   4
months.size(): 4
prices.size(): 4


Jan 2019: 100
Feb 2019: 100
March 2019: 200
April 2019: 300

关于java - 将连续的字符串数据拆分为所需的垂直输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56944758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com