gpt4 book ai didi

Java RegEx - 用开始和结束分割段落的正则表达式

转载 作者:行者123 更新时间:2023-12-03 00:57:56 27 4
gpt4 key购买 nike

我是java正则表达式的新手。请帮助我。考虑下面的段落,

段落:

            Name abc
sadghsagh
hsajdjah Name
ggggggggg
!!!
Name ggg
dfdfddfdf Name
!!!
Name hhhh
sahdgashdg Name
asjdhjasdh
sadasldkalskd
asdjhakjsdhja
!!!

我需要将上面的段落拆分为以名称开头并以 !!! 结尾的文本 block 。这里我不想用!作为分割段落的唯一分隔符。我还需要在我的正则表达式中包含起始序列(名称)。

即,我的结果 api 应该类似于 SplitAsBlocks("Paragraph","startswith Name","endswith!!!”)

如何实现这一点,请任何人帮助我......

现在我想要与 Brito 给出的输出相同的输出...但在这里我在“hsajdjah”之后添加了名称。这里它分割文本如下:

Name
ggggggggg
!!!

但我需要

Name abc
sadghsagh
hsajdjah Name
ggggggggg
!!!

也就是说,我必须匹配位于行开头而不是中间的名称。

请推荐我...

Bart ...请参阅下面的代码输入案例...

我需要使用带有参数 start => Name 和 end => 的 API 来分割以下内容!但输出各不相同..我只有 3 个以 Name 开头并以 ! 结尾的 block 。 。我也附上了输出。

String myInput =    "Name hhhhh class0"+ "\n"+
"HHHHHHHHHHHHHHHHHH"+ "\n"+
"!"+ "\n"+
"Name TTTTT TTTT"+ "\n"+
"GGGGGG UUUUU IIII"+ "\n"+
"!"+ "\n"+
"Name JJJJJ WWWW"+ "\n"+
"IIIIIIIIIIIIIIIIIIIII"+ "\n"+
"!"+ "\n"+
"RRRRRRRRRRR TTTTTTTT"+ "\n"+
"HHHHHH"+ "\n"+
"JJJJJ 1 Name class1"+ "\n"+
"LLLLL 5 Name class5"+ "\n"+
"!"+ "\n"+
"OOOOOO HHHH FFFFFF"+ "\n"+
"service 0 Name class12"+ "\n"+
"!"+ "\n"+
"JJJJJ YYYYYY 3/0"+ "\n"+
"KKKKKKK"+ "\n"+
"UUU UUU UUUUU"+ "\n"+
"QQQQQQQ"+ "\n"+
"!";
String[] tokens = tokenize(myInput, "Name", "!");
int n = 0;
for(String t : tokens) {
System.out.println("---------------------------\n"+(++n)+"\n"+t);
}

输出:

---------------------------
1
Name hhhhh class0
HHHHHHHHHHHHHHHHHH
!
---------------------------
2
Name TTTTT TTTT
GGGGGG UUUUU IIII
!
---------------------------
3
Name JJJJJ WWWW
IIIIIIIIIIIIIIIIIIIII
!
---------------------------
4
Name class1
LLLLL 5 Name class5
!
---------------------------
5
Name class12
!

这里我只需要在行的开头而不是中间有名称......如何为此添加正则表达式...

最佳答案

尝试:

import java.util.*;
import java.util.regex.*;

public class Main {

public static String[] tokenize(String text, String start, String end) {
// old line:
//Pattern p = Pattern.compile("(?s)"+Pattern.quote(start)+".*?"+Pattern.quote(end));
// new line:
Pattern p = Pattern.compile("(?sm)^"+Pattern.quote(start)+".*?"+Pattern.quote(end)+"$");

Matcher m = p.matcher(text);
List<String> tokens = new ArrayList<String>();
while(m.find()) {
tokens.add(m.group());
}
return tokens.toArray(new String[]{});
}

public static void main(String[] args) {
String text = "Name abc" + "\n" +
"sadghsagh" + "\n" +
"hsajdjah Name" + "\n" +
"ggggggggg" + "\n" +
"!!!" + "\n" +
"Name ggg" + "\n" +
"dfdfddfdf Name" + "\n" +
"!!!" + "\n" +
"Name hhhh" + "\n" +
"sahdgashdg Name" + "\n" +
"asjdhjasdh" + "\n" +
"sadasldkalskd" + "\n" +
"asdjhakjsdhja" + "\n" +
"!!!";
String[] tokens = tokenize(text, "Name", "!!!");
int n = 0;
for(String t : tokens) {
System.out.println("---------------------------\n"+(++n)+"\n"+t);
}
}
}

关于Java RegEx - 用开始和结束分割段落的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1706775/

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