Java字符串搜索 – 在字符串两侧搜索动态长度的文本

huangapple go评论76阅读模式
英文:

Java String Search - Search text of dynamic length on either side of string

问题

我有一个问题我正在尝试查找竖线两侧的文本

这是我添加到数组的字符串
[NoMDO200AbcN-497218|AU59800987619804277voucherCustomer00:00:0016.10.2020]

我想找到竖线 497218|AU59 两侧的文本

注意数组值的索引始终会变化但带有竖线的字符串始终可用因此问题中的字符串从不具有相同的索引这意味着我无法在整个字符串上进行搜索但我可以找到竖线的索引

将其添加到列表后提取值对我不起作用因为索引始终会更改

List<String> instanceListSplit = Arrays.asList(iList.split(" "));

因为在屏幕上它们由空格分隔所以我将整个字符串添加到了数组中

String[] instanceSplitToArray = (String[]) Arrays.asList(iList.split(" ")).toArray();

对于我的情况这可能不起作用因为我需要竖线 | 两侧的文本

arrayListmy.add(iList.substring(iList.lastIndexOf("|")));

请问有人可以告诉我如何获取竖线 497218|AU59 两侧的文本这些文本的长度也不固定这意味着左侧的文本可以有任意数量的字符

List<String> instanceList = Arrays.asList(bulkGridInfo.split("\n"));

for (String iList : instanceList){
   String[] instanceSplitToArray = (String[]) Arrays.asList(iList.split(" ")).toArray();
   List<String> instanceListSplit = Arrays.asList(iList.split(" "));
   ArrayUtils.reverse(toReverse);
   System.out.println(Arrays.toString(toReverse));
   arrayListmy.add(iList.substring(iList.lastIndexOf("|")).replace("|",""));
}

arrayListmy的值是"AU59",这是可以找到的,但我还需要竖线左侧的数字文本,它的大小可以是任意的。我认为将其反转可能更容易找到文本,但这让我陷入了相同的挑战。

英文:

I have a question. I am trying to search for the text on either side of the |.

This is the string which I have added to an array
[No, MDO, 200, AbcN, -, 497218|AU59, 800987619804277, voucher, Customer, 00:00:00, 16.10.2020]

I would like to find these text on either side of the pipe 497218|AU59

Note: The indexes always changes for the array values but the string with the pipe is always available, so the string in question never has the same index which means I cannot search on the entire string but I can find the index of |.

Extracting the values after adding to List does not work for me because indexes always changes:

List&lt;String&gt; instanceListSplit = Arrays.asList(iList.split( &quot; &quot;));

I have added the entire string to an array because on the screen they are separated by spaces:

String[] instanceSplitToArray = (String[]) Arrays.asList(iList.split( &quot; &quot;)).toArray();

This might not work for my scenario because I need the text on either side of the pipe | :

arrayListmy.add(iList.substring(iList.lastIndexOf(&quot;|&quot;)));

Can someone please tell me how I can get the text on either side of the pipe, 497218|AU59. These text do not have set Lengths either, meaning that the text on the left can have -n amount of characters.

List&lt;String&gt; instanceList = Arrays.asList(bulkGridInfo.split( &quot;\n&quot;));
for (String iList : instanceList){
String[] instanceSplitToArray = (String[]) Arrays.asList(iList.split( &quot; &quot;)).toArray();
List&lt;String&gt; instanceListSplit = Arrays.asList(iList.split( &quot; &quot;));
ArrayUtils.reverse(toReverse);
System.out.println(Arrays.toString(toReverse));
arrayListmy.add(iList.substring(iList.lastIndexOf(&quot;|&quot;)).replace(&quot;|&quot;,&quot;&quot;));
}

The arrayListmy value is AU59, which is fine i can find it but I also need the number text to the left of the pipe which can be any length in size. I thought reversing it might be easier to find the text but gets me in the same challenge.

答案1

得分: 2

你可以使用正则表达式来提取竖线两侧的字符串。

例如:

public static void main(String[] args) {
    String input = "[No, MDO, 200, AbcN, -, 497218|AU59, "
            + "800987619804277, voucher, Customer, 00:00:00, 16.10.2020]";
    Pattern p = Pattern.compile(".*?(\\w+)\\|(\\w+).*");
    Matcher matcher = p.matcher(input);
    matcher.find();
    System.out.println("竖线前的文本:" + matcher.group(1));
    System.out.println("竖线后的文本:" + matcher.group(2));
}

输出:

竖线前的文本:497218
竖线后的文本:AU59
英文:

You could use a Regex to extract the Strings on both sides of the pipe.

For example:

public static void main(String[] args) {
String input = &quot;[No, MDO, 200, AbcN, -, 497218|AU59, &quot;
+ &quot;800987619804277, voucher, Customer, 00:00:00, 16.10.2020]&quot;;
Pattern p = Pattern.compile(&quot;.*?(\\w+)\\|(\\w+).*&quot;);
Matcher matcher = p.matcher(input);
matcher.find();
System.out.println(&quot;Text before pipe: &quot; + matcher.group(1));
System.out.println(&quot;Text after pipe: &quot; + matcher.group(2));
}

Output:

Text before pipe: 497218
Text after pipe: AU59

答案2

得分: 1

你可以使用基于字符串的操作来完成。

 String str = "[No, MDO, 200, AbcN, -, 497218|AU59, 800987619804277, voucher, Customer, 00:00:00, 16.10.2020]";
String[] strings = str.split("\\|");
String firstNumber = strings[0].substring(strings[0].lastIndexOf(",") + 2); // 这里使用2是因为1是逗号,第二个是空格
String secondNumber = strings[1].substring(0, strings[1].indexOf(","));
System.out.println(firstNumber + " " + secondNumber);
英文:

you can do it with String based operations

 String str = &quot;[No, MDO, 200, AbcN, -, 497218|AU59, 800987619804277, voucher, Customer, 00:00:00, 16.10.2020]&quot;;
String[] strings = str.split(&quot;\\|&quot;);
String firstNumber = strings[0].substring(strings[0].lastIndexOf(&quot;,&quot;) + 2); // we are using 2 because 1 is the comma and second is the white space
String secondNumber = strings[1].substring(0,strings[1].indexOf(&quot;,&quot;));
System.out.println(firstNumber + &quot; &quot; + secondNumber);

答案3

得分: 1

迭代解决方案

简单的迭代解决方案:

String value = "[No, MDO, 200, AbcN, -, 497218|AU59, 800987619804277, voucher, Customer, 00:00:00, 16.10.2020]";
String[] tokens = value.split("\\[|\\]|,\\s+");
String pipeItem = "";
for (String token : tokens) {
    if (token.contains("|")) {
        pipeItem = token;
        break;
    }
}
String[] pipeTokens = pipeItem.split("\\|");
System.out.println("first token: " + pipeTokens[0]);  // 左侧部分
System.out.println("second token: " + pipeTokens[1]); // 右侧部分

声明式解决方案

这也可以使用流(Streams)一行代码完成:

String value = "[No, MDO, 200, AbcN, -, 497218|AU59, 800987619804277, voucher, Customer, 00:00:00, 16.10.2020]";
String [] pipeTokens = Arrays.stream(value.split("\\[|\\]|,\\s+"))
        .filter(item -> item.contains("|"))
        .findFirst()
        .map(item -> item.split("\\|"))
        .get();

System.out.println("first token: " + pipeTokens[0]);  // 左侧部分
System.out.println("second token: " + pipeTokens[1]); // 右侧部分
英文:

Iterative solution

Trivial iterative solution:

String value = &quot;[No, MDO, 200, AbcN, -, 497218|AU59, 800987619804277, voucher, Customer, 00:00:00, 16.10.2020]&quot;;
String[] tokens = value.split(&quot;\\[|\\]|,\\s+&quot;);
String pipeItem = &quot;&quot;;
for (String token : tokens) {
    if (token.contains(&quot;|&quot;)) {
        pipeItem = token;
        break;
    }
}
String[] pipeTokens = pipeItem.split(&quot;\\|&quot;);
System.out.println(&quot;first token: &quot; + pipeTokens[0]);  // left part
System.out.println(&quot;second token: &quot; + pipeTokens[1]); // right part

Declarative solution

This also could be done with Streams as one liner:

String value = &quot;[No, MDO, 200, AbcN, -, 497218|AU59, 800987619804277, voucher, Customer, 00:00:00, 16.10.2020]&quot;;
String [] pipeTokens = Arrays.stream(value.split(&quot;\\[|\\]|,\\s+&quot;))
        .filter(item -&gt; item.contains(&quot;|&quot;))
        .findFirst()
        .map(item -&gt; item.split(&quot;\\|&quot;))
        .get();

System.out.println(&quot;first token: &quot; + pipeTokens[0]);  // left part
System.out.println(&quot;second token: &quot; + pipeTokens[1]); // right part

huangapple
  • 本文由 发表于 2020年9月23日 15:26:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64023015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定