Substring in Java UDF

Kaushik :

I want to extract words in front of "Curncy" using substring and indexOf or by any other recommended way.

AED Curncy|0|6|UAE Dirham Spot|3.67300|12/12/2016|1.0000|N|USDAUD Curncy|0|6|USD-AUD X-RATE|1.385650|12/12/2018|1.0000|N|BGN Curncy|0|6|Bulgarian Lev Spot|1.720302|12/12/2015|1.0000|N|

I want values to be populated as:

AED
USAAUD
BGN

Kindly let me know how to get the desired output.

vicpermir :

One way to do it is with a very simple regular expression.

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    private static final Pattern PATTERN = Pattern.compile("(\\w*) Curncy");

    public static void main(String[] args) {
        String str = "AED Curncy|0|6|UAE Dirham Spot|3.67300|12/12/2016|1.0000|N|USDAUD Curncy|0|6|USD-AUD X-RATE|1.385650|12/12/2018|1.0000|N|BGN Curncy|0|6|Bulgarian Lev Spot|1.720302|12/12/2015|1.0000|N|";

        List<String> matches = new ArrayList<String>();
        Matcher m = PATTERN.matcher(str);
        while (m.find()) {
            matches.add(m.group(1));
        }

        System.out.println(matches);
    }
}

This will print the List of matches like so:

[AED, USDAUD, BGN]

More info on how to use regular expression patterns in Java.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27819&siteId=1