简体   繁体   English

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

[英]How to get value from string template in Java

I have a String template like this: 我有一个像这样的字符串模板:

"Thanks, this is your value : [value]. And this is your account number : [accountNumber]" “谢谢,这是您的值:[值]。这是您的帐号:[accountNumber]”

And i have inputs like this: 我有这样的输入:

input 1 : "Thanks, this is your value : 100. And this is your account number : 219AD098" 输入1:“谢谢,这是您的值:100。这是您的帐号:219AD098”

input 2 : "Thanks, this is your value : 150. And this is your account number : 90582374" 输入2:“谢谢,这是您的值:150。这是您的帐号:90582374”

input 3 : "Thanks, this is your value : 200. And this is your account number : 18A47" 输入3:“谢谢,这是您的值:200。这是您的帐号:18A47”

I want output like this: 我想要这样的输出:

output 1 : "[value] = 100 | [accountNumber] = 219AD098" 输出1:“ [值] = 100 | [帐户数] = 219AD098”

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

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

How to do that? 怎么做? Maybe using Regex? 也许使用正则表达式?

note : the template is not fixed.. the only thing that fixed is [value] and [accountNumber].. 注意:模板不是固定的。唯一固定的是[value]和[accountNumber]。

use this regex 使用此regex

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

this will extract both the values from the lines that you want. 这将从所需的行中提取两个值。 after getting them you can concatenate them with anything you want like your output string. 获得它们之后,您可以将它们与所需的任何内容(例如输出字符串)连接起来。

the code to use this regex will be like this 使用此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());
}

so this way you will get the matched values in this array list, of you can use a simple array if you prefer. 这样,您将在此数组列表中获得匹配的值,如果愿意,可以使用一个简单的数组。

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

is is easy to do without regex too: 没有正则表达式也很容易做到:

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++;
}

Try this, StringUtils.subStringBefore 试试这个, 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]);

You can use regular expression. 您可以使用正则表达式。

Here is full example 这是完整的例子

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;

    }

}

Output 产量

Value: 100 
Account :  219AD098

Just check it out. 只是检查出来。

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: Value : 100 O / P:值:100
Account Number : 219AD098 帐号:219AD098

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM