Ne7WoRK
Ne7WoRK

Reputation: 161

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

Upvotes: 2

Views: 1053

Answers (4)

The fourth bird
The fourth bird

Reputation: 163362

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]

Upvotes: 0

edjm
edjm

Reputation: 5472

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

Upvotes: 0

Dang Nguyen
Dang Nguyen

Reputation: 1209

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

Upvotes: 5

devgianlu
devgianlu

Reputation: 1580

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
}

Upvotes: 1

Related Questions