英文:
Java String Search - Search text of dynamic length on either side of string
问题
我有一个问题。我正在尝试查找竖线两侧的文本。
这是我添加到数组的字符串
[No,MDO,200,AbcN,-,497218|AU59,800987619804277,voucher,Customer,00:00:00,16.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<String> instanceListSplit = Arrays.asList(iList.split( " "));
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( " ")).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("|")));
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<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("|",""));
}
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 = "[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("Text before pipe: " + matcher.group(1));
System.out.println("Text after pipe: " + 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 = "[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); // we are using 2 because 1 is the comma and second is the white space
String secondNumber = strings[1].substring(0,strings[1].indexOf(","));
System.out.println(firstNumber + " " + 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 = "[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]); // left part
System.out.println("second token: " + pipeTokens[1]); // right part
Declarative solution
This also could be done with Streams as one liner:
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]); // left part
System.out.println("second token: " + pipeTokens[1]); // right part
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论