简体   繁体   中英

Need help forming a regex pattern matcher

My String to parse is an http response with the separation of vertical bars '|' colons ':' and commas ','

"1701|919422522891:224c1214-bb95-414d-ba76-77db95370545,1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6"

I have made an attempt to parse with the following code

public void split2(){
    String input = "1701|919422522891:224c1214-bb95-414d-ba76-77db95370545," +
                   "1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6";

    Matcher matcher = Pattern.compile("\\|.*?\\:\\,").matcher(input);
    int nr = 0;
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}

UPDATE - WORKING CODE

The split method works well

  public String[] split(String regex,String input)
    {
        input = "1701|919422522891:224c1214-bb95-414d-ba76-77db95370545," +
                "1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6";
        regex = "\\||:|," ;


        String[] soso = Pattern.compile(regex).split(input, input.length());

            for(String s :soso){
                Log.e("",s.toString());
        }

        return null;
    }

You can use:

Matcher matcher = Pattern.compile("[^|]*\\|[^:]*:[^,]*,").matcher(input);

Explanation: This regex "[^|]*\\\\|[^:]*:[^,]*," means:

  1. [^|]* - Match 0 or more characters before a pipe
  2. \\\\| - Match a pipe
  3. [^:]* - Match 0 or more characters before a colon
  4. : - Match a colon
  5. [^,]* - Match 0 or more characters before a comma
  6. , - Match a coma

Instead of match you can easily do a split.

\||:|,

See demo.

https://regex101.com/r/vN3sH3/28

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