2

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

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

我有这样的输入:

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

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

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

我想要这样的输出:

输出 1:“[值] = 100 | [帐号] = 219AD098”

输出 2:“[值] = 150 | [帐号] = 90582374”

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

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

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

4

6 回答 6

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());
}

因此,您将在此数组列表中获得匹配的值,如果您愿意,可以使用简单的数组。

于 2013-08-27T07:51:16.090 回答
1
    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);
于 2013-08-27T07:50:45.483 回答
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
于 2013-08-27T08:30:15.307 回答
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++;
}
于 2013-08-27T07:55:58.237 回答
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]);
于 2013-08-27T07:58:27.693 回答
-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

于 2017-05-16T14:15:38.337 回答