如何从Java中的字符串模板获取值

时间:2013-08-27 07:42:56

标签: java regex string

我有一个像这样的字符串模板:

  

“谢谢,这是您的价值:[价值]。这是您的帐号:[accountNumber]”

我有这样的输入:

  

输入1:“谢谢,这是你的价值:100。这是你的账号:219AD098”

     

输入2:“谢谢,这是你的价值:150。这是你的账号:90582374”

     

输入3:“谢谢,这是你的价值:200。这是你的账号:18A47”

我想要这样的输出:

  

输出1:“[value] = 100 | [accountNumber] = 219AD098”

     

输出2:“[value] = 150 | [accountNumber] = 90582374”

     

输出3:“[value] = 200 | [accountNumber] = 18A47”

怎么做?也许使用正则表达式?

注意:模板不固定..唯一修复的是[value]和[accountNumber] ..

6 个答案:

答案 0 :(得分:4)

使用此regex

(?<=value : )(\d+)|(?<=number : )(.+)(?=")

这将从您想要的行中提取两个值。获取它们之后,您可以将它们与您想要的任何内容(如输出字符串)连接起来。

使用此regex的代码将是这样的

Pattern pattern = Pattern.compile("(?<=value : )(\d+)|(?<=number : )(.+)(?=\")");
Matcher matcher = pattern.matcher(SOURCE_TEXT_LINE);
List<String> allMatches = new ArrayList<String>();
while (matcher.find()) {
 allMatches.add(matcher.group());
}

所以这样你就可以得到这个数组列表中的匹配值,如果你愿意,你可以使用一个简单的数组。

答案 1 :(得分:0)

    String text = "Thanks, this is your value : 100. And this is your account number : 219AD098";
    Pattern pattern = Pattern
            .compile("Thanks, this is your value : (\\d+). And this is your account number : (\\w+)");
    Matcher matcher = pattern.matcher(text);
    matcher.find();
    String outputText = "[value] = " + matcher.group(1)
            + " | [accountNumber] = " + matcher.group(2);
    System.out.println(outputText);

答案 2 :(得分:0)

如果没有正则表达式,

很容易做到:

String input = getInput();

String[] inputLines = input.split("\n");
String output = "";
int counter = 1;

for(string line : inputLines)
{
   int subValStart = line.indexOf("value : ");
   string val = line.substring(subValStart, line.indexOf("|") - subValStart);
   string accNum = line.substring("account number : ");
   output += "output " + counter + " :\"[value] = "+ val + " | [accountNumber] = " + accNum + "\"\n"; 
   counter++;
}

答案 3 :(得分:0)

试试这个,StringUtils.subStringBefore

   String sCurrentLine = CURRENT_LINE;
   String[] splitedValue = sCurrentLine.split(":");

   StringBuilder stringBuilder = new StringBuilder();
   stringBuilder.append(splitedValue[0].replace("input", "output"));
   stringBuilder.append(": \"[value] = "+StringUtils.substringBefore(splitedValue[2], "."));
   stringBuilder.append(" | [accountNumber] = "+splitedValue[3]);

答案 4 :(得分:0)

您可以使用正则表达式。

这是完整的例子

package snippet;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class Test {

    public static void main(String[] args) throws CoffeeDOMException, IOException {
        String test = "Thanks, this is your value : 100 . And this is your account number : 219AD098";
        String valueExpression = "\\svalue\\s:([^.]+)";
        String accExpresion = "\\saccount\\snumber\\s:([^$]+)";
        System.out.println("Value:" + runSubRegex(valueExpression, test));
        System.out.println("Account:" + runSubRegex(accExpresion, test));

    }

    private static String runSubRegex(String regex, String tag) {
        Pattern p = Pattern.compile(regex);
        Matcher matcher = p.matcher(tag);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;

    }

}

输出

Value: 100 
Account :  219AD098

答案 5 :(得分:-1)

请检查一下。

String template = "Thanks, this is your value : -XXXX-. And this is your account number : -XXXX- -XXXX- Value,Account Number";
String input = "Thanks, this is your value : 100. And this is your account number : 219AD098";

/*String template = "You can use -XXXX- mehod to read values from -XXXX- Value 1,value 2";
String input = "You can use this mehod to read values from custom string template";*/

String[] splitValue = template.split("-XXXX-");
for (String splitValueTemp : splitValue) {
    input = input.replace(splitValueTemp, "!");
}

List<String> value = Arrays.asList(input.split("!"));
List<String> Key = Arrays.asList(splitValue[splitValue.length - 1].split(","));
if (value != null && value.size() > 1) {
    int iCnt = 0;
    for (String opValue : value.subList(1, value.size())) {
        if (Key.size() > iCnt) {
            System.out.println(Key.get(iCnt).trim() + " : " + opValue.trim());
        }
        iCnt++;
    }
}

O / P: 价值:100
帐号:219AD098