简体   繁体   中英

Java regex. How to get 3 different parts of text?

I have this very long String in java

200/23/Ne7WoRK/3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18>a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a>03cf334295615f981c47e

What I want to achieve is to get:

Bid: 200

Username: Ne7WoRK

Signature: 3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a03cf334295615f981c47e

I need 3 regular expressions that will help me get separate Strings of the bid value, username and Signature. I am not sure how to achieve that. My attempt to solve this was with the following regular expression

\\b.*/\\b

However, this regular expression matches the whole 3 subparts and gives an output of this

200/23/Ne7WoRK/

I am not sure how to create 3 different regular expressions where:

  1. The first one will match any digits from the start of the string up to the first "/" symbol. Giving String of 200
  2. The second one will match any character from the second "/" symbol up to the third "/" symbol. Giving Ne7WoRK
  3. The third one should match everything from the third "/" up to the end of the string. Giving the long number of - 3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a03cf334295615f981c47e

you can split it

String a = "200/23/Ne7WoRK/3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18>a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a>03cf334295615f981c47e";
System.out.println(Arrays.toString(a.split("/")));

Result

[200, 23, Ne7WoRK, 3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18>a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a>03cf334295615f981c47e]

And then do some other work to get the wanted requirement

Try this: (\\d+)\\/(?:.+)\\/(.+)\\/(.+)

It'll give you 3 groups containing the 3 strings.

The Java code for this would be:

Matcher matcher = Pattern.compile("(\d+)\/(?:.+)\/(.+)\/(.+)").matcher(yourString);
if (matcher.find()) {
    String bid = matcher.group(1);
    String username = matcher.group(2);
    String signature = matcher.group(3);
} else {
    // Malformed String
}

You can group the expressions by using ( ) for example you'd have 3 groupings.

^([\d]*)\/([\d]*)\/([a-zA-Z|0-9]*)
  • Group 1: Digits
  • Group 2: Digits
  • Group 3: Alpha & Digits

You could split on matching either a forward slash, 1+ digits and a forward slash or just a forward slash using an alternation :

/\d+/|/

Regex demo | Java demo

For example:

String regex = "/\\d+/|/";
String string = "200/23/Ne7WoRK/3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18>a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a>03cf334295615f981c47e";
System.out.println(Arrays.toString(string.split(regex)));

Result:

[200, Ne7WoRK, 3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18>a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a>03cf334295615f981c47e]

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