gpt4 book ai didi

java - 仅从字符串中检索给定的单词

转载 作者:行者123 更新时间:2023-11-30 10:57:09 26 4
gpt4 key购买 nike

例如,我正在处理 boolean 查询:

Information mining AND retrieval OR position

我想创建两个数组或列表,一个包含术语(术语可以有大写字母)术语可以有短语查询,即多个单词。

{Information mining, retrieval, position}

第二个数组将包含关键字

{AND, OR}

这样我就可以将相关的搜索词与其 boolean 运算符对应起来。

我使用正则表达式“AND | NOT | OR”创建了第一个数组
我想不出从给定字符串创建第二个数组的方法。

我可以使用什么正则表达式来获得结果?

最佳答案

您可以像这样使用正则表达式:

([A-Z][a-z\s]+)|([A-Z]+)
^^^^^^^^^^^^^ ^--- Capture wey words
+--- Capture regular words

Regex Working demo

Java demo example

如下所示,第 1 组将捕获单词,而第 2 组将捕获关键字。

enter image description here

比赛信息

MATCH 1
1. [0-11] `information`
MATCH 2
2. [12-15] `AND`
MATCH 3
1. [16-25] `retrieval`
MATCH 4
2. [26-28] `OR`
MATCH 5
1. [29-37] `position`

关于java - 仅从字符串中检索给定的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32742252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com