简体   繁体   中英

Using regex to extract two words from a string

I have tried to look at several examples but can't figure out how or what the best way to get two values from strings like this:

setEditMode                                    | Ändringsläge

I can get the first value but how do I get the second one after the "|"?

I have tried using Pattern and Matcher and also using split().

 public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("C:\\Temp\\translate\\en.lang"));
            String line;
            while ((line = br.readLine()) != null) {

                String[] values = line.split("\\|");
                String v1 = values[0];
                String v2 = values[1];
                System.out.println(v2);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

java.lang.ArrayIndexOutOfBoundsException: 1

When just printing "line" in the System.out i get:

archiveLoad                                      | Loading archives
archiveLoadCount                                 | Archives uploaded:
archiveLoadStage                                 | Stage:
archiveLoadFilesCount                            | Files uploaded:
archiveLoadElapsedTime                           | Time elapsed:
archiveLoadInit                                  | Initialization
archiveLoadUndeletableIntervals                  | Loading undeletable intervals
archiveLoadDb                                    | Loading from database
archiveLoadDisk                                  | Loading from disk
archiveLoadProblemIntervals                      | Loading data about problems

You can use split, as you shown in a comment

String line = "aaaa |  aaaa";
String[] values = line.split("\\|");
String v1 = values[0];
String v2 = values[1];

You can also use a regex that captures twice all but | : ([^|]+)|([^|]+)

This depends on how you want your output, but it can easily be done using something like:

([a-zA-ZåäöÖÄÅ]+|[a-zA-ZåäöÖÄÅ]+) .

which will match all letter, including åäö/ÅÄÖ. Add 0-9 to your regex to also match numbers:

( ([0-9a-zA-ZåäöÖÄÅ]+|[0-9a-zA-ZåäöÖÄÅ]+) )

Live example: http://rubular.com/r/xMZ1na9r0q

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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