Java RegEx - 用开始和结束拆分段落的正则表达式 [英] Java RegEx - Regular expression to split a paragraph with start and end

查看:112
本文介绍了Java RegEx - 用开始和结束拆分段落的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java regex的新手。请帮帮我。
考虑以下段落,

I am new to java regex.Please help me. Consider the below paragraph,

段落:

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

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

i need to split the above paragraph as blocks of text starting with Name and ending with !!! . Here I dont want to use !!! as the only delimiter to split the paragraph.I need to include the starting sequence (Name) also in my regex.

ie。,我的结果api应该看起来像SplitAsBlocks( 段落,以姓名开头,以
结尾!!!

ie., my result api should looks like SplitAsBlocks("Paragraph","startswith Name","endswith !!!")

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

How to achieve this ,please anyone help me ...

现在我想要与Brito给出相同的输出...但是在这里我在hsajdjah之后添加了Name。这里它将文本拆分为beow:

Now i want the same output as Brito given ...but here i have added Name after "hsajdjah".Here it split the text as beow :

Name
ggggggggg
!!!

但我需要

Name abc
sadghsagh
hsajdjah Name
ggggggggg
!!!

这是我必须匹配名称,这是在行的开头,而不是在中间。

that is i have to match up Name which is at the starting of the line ,not in the middle .

请建议我......

please suggest me ...

Bart ...请参阅以下输入大小写代码。 ..

Bart ...see the below input case for your code ...

我需要使用参数start => Name和end =>的ur API拆分以下内容!
但输出变化..i只有3个块以Name开头并以!结尾! 。
i也附加了输出。

i need to split the following using ur API with parameter start => Name and end => ! But the output varies ..i have only 3 blocks starts with Name and ends with ! . i have attached the output also .

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);
    }

OutPut:

---------------------------
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
!

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

Here i need to have only the Name at the starting of the line not at the middle ... How to add regex for this ...

推荐答案

尝试:

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 - 用开始和结束拆分段落的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆