Reputation: 161
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:
Upvotes: 2
Views: 1053
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+/|/
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
Reputation: 5472
You can group the expressions by using ( ) for example you'd have 3 groupings.
^([\d]*)\/([\d]*)\/([a-zA-Z|0-9]*)
Upvotes: 0
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
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