简体   繁体   English

Java正则表达式。 如何获得文本的3个不同部分?

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

I have this very long String in java 我在Java中有这个很长的字符串

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

What I want to achieve is to get: 我要实现的目标是:

Bid: 200 竞标价格:200

Username: Ne7WoRK 用户名:Ne7WoRK

Signature: 3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a03cf334295615f981c47e 签名:3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a03cf334295615f981c47e

I need 3 regular expressions that will help me get separate Strings of the bid value, username and Signature. 我需要3个正则表达式,可以帮助我分别获取出价值,用户名和签名的字符串。 I am not sure how to achieve that. 我不确定如何实现这一目标。 My attempt to solve this was with the following regular expression 我试图解决此问题的方法是使用以下正则表达式

\\b.*/\\b \\湾* / \\ b

However, this regular expression matches the whole 3 subparts and gives an output of this 但是,此正则表达式匹配整个3个子部分,并给出了此输出

200/23/Ne7WoRK/ 23分之200/ Ne7WoRK /

I am not sure how to create 3 different regular expressions where: 我不确定如何创建3个不同的正则表达式,其中:

  1. The first one will match any digits from the start of the string up to the first "/" symbol. 第一个将匹配从字符串开头到第一个“ /”符号的任何数字。 Giving String of 200 给200的弦
  2. The second one will match any character from the second "/" symbol up to the third "/" symbol. 第二个将匹配从第二个“ /”符号到第三个“ /”符号的任何字符。 Giving Ne7WoRK 给Ne7WoRK
  3. The third one should match everything from the third "/" up to the end of the string. 第三个应该匹配从第三个“ /”到字符串末尾的所有内容。 Giving the long number of - 3045022100d62568e28cb58b4a5308750e63e4690c4538ddc18a9dc6075d02f7b4f942c4aa0220587350e7db1f4380a36ebb441906833563d32a62c4a03cf334295615f981c47e 提供大量的-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+)\\/(?:.+)\\/(.+)\\/(.+) 试试这个: (\\d+)\\/(?:.+)\\/(.+)\\/(.+)

It'll give you 3 groups containing the 3 strings. 它会给您3组,包含3个字符串。

The Java code for this would be: Java代码为:

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. 您可以使用()对表达式进行分组,例如,您可以分为3个分组。

^([\d]*)\/([\d]*)\/([a-zA-Z|0-9]*)
  • Group 1: Digits 第一组:数字
  • Group 2: Digits 第二组:数字
  • Group 3: Alpha & Digits 第3组:字母和数字

You could split on matching either a forward slash, 1+ digits and a forward slash or just a forward slash using an alternation : 您可以使用交替符号来匹配正斜杠,1 +数字和正斜杠,或者仅匹配正斜杠:

/\d+/|/

Regex demo | 正则表达式演示 | Java demo Java演示

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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