java中的模式正则表达式

java中的模式正则表达式,java,Java,我正在尝试使用java实现模式regx,在某种程度上我也这么做了,但我需要一点帮助,即我正在尝试匹配两种模式,即名称和日期(来自输入txt文件),我正在将它们存储在arraylist中,并尝试获得类似名称的输出;日期我也能实现,但我想要的输出并不是我所期望的,也许从这个例子中你会清楚我真正想要的是什么: 输入文件: Hey my name is alex and my date of birth is 19-may-1999 Hey my name is raman and my date of

我正在尝试使用java实现模式regx,在某种程度上我也这么做了,但我需要一点帮助,即我正在尝试匹配两种模式,即名称和日期(来自输入txt文件),我正在将它们存储在arraylist中,并尝试获得类似名称的输出;日期我也能实现,但我想要的输出并不是我所期望的,也许从这个例子中你会清楚我真正想要的是什么:

输入文件:

Hey my name is alex and my date of birth is 19-may-1999
Hey my name is raman and my date of birth is 21-may-1999
Hey my name is ali and my date of birth is 
Hey my name is jack and my date of birth is 23-may-1999
我在控制台上获得的输出:

alex ; 19-may-1999
raman ; 21-may-1999
ali ; 23-may-1999

Output that i want :
alex ; 19-may-1999
raman ; 21-may-1999
ali ; 
jack ; 23-may-1999
因为没有提到阿里的出生日期,所以我想在他出生的地方留个空白

请注意几点: -不要担心模式,因为输入文件数据不是我实际传递的东西,这只是一个示例,向您展示我真正想要实现的目标 -请让我知道,如果我的问题是含糊不清的,需要一些编辑

提前谢谢

这是我的片段

    public static void getFinalResult(String filepath)
{

    List<String> StreamMatcher = new ArrayList<String>();
    List<String> DateMatcher = new ArrayList<String>();
    int index = 0;
    try
    {

        FileInputStream fstream = new FileInputStream(filepath);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null)
        {

            Pattern p = Pattern.compile("[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4}");
            Matcher m = p.matcher(strLine);
            while (m.find())
            {
                String inp1 = m.group(0);
                DateMatcher.add(inp1);

            }
        }
        br.close();
    }
    catch (Exception ex)

    {
        System.err.println("Error: " + ex.getMessage());
    }

    try
    {
        FileInputStream fstream1 = new FileInputStream(filepath);
        DataInputStream in1 = new DataInputStream(fstream1);

        BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
        String strLine1;

        while ((strLine1 = br1.readLine()) != null)
        {

            Pattern p = Pattern.compile("Stream:(.*) \"(.*)\"");

            Matcher m = p.matcher(strLine1);

            while (m.find())
            {
                String inp2 = m.group(2);
                StreamMatcher.add(inp2);
                System.out.println(StreamMatcher.get(index) + " ; " + DateMatcher.get(index));
                index++;

            }

        }
        br1.close();

    }
    catch (Exception e)
    {
        System.err.println(" ");
    }

}
publicstaticvoid getFinalResult(字符串文件路径)
{
List StreamMatcher=new ArrayList();
List DateMatcher=new ArrayList();
int指数=0;
尝试
{
FileInputStream fstream=新的FileInputStream(filepath);
DataInputStream in=新的DataInputStream(fstream);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦斯特林;
而((strLine=br.readLine())!=null)
{
Pattern p=Pattern.compile(“[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4}”);
Matcher m=p.Matcher(strLine);
while(m.find())
{
字符串inp1=m.group(0);
DateMatcher.add(inp1);
}
}
br.close();
}
捕获(例外情况除外)
{
System.err.println(“错误:+ex.getMessage());
}
尝试
{
FileInputStream fstream1=新的FileInputStream(filepath);
DataInputStream in1=新的DataInputStream(fstream1);
BufferedReader br1=新的BufferedReader(新的InputStreamReader(in1));
字符串strLine1;
而((strLine1=br1.readLine())!=null)
{
模式p=Pattern.compile(“流:(.*)\”(.*)\”;
Matcher m=p.Matcher(strLine1);
while(m.find())
{
字符串inp2=m.group(2);
StreamMatcher.add(inp2);
System.out.println(StreamMatcher.get(index)+“;”+DateMatcher.get(index));
索引++;
}
}
br1.close();
}
捕获(例外e)
{
System.err.println(“”);
}
}
如果没有匹配项,可以添加空字符串

if (!m.matches()) {
    DateMatcher.add("");
} else {
    String inp1 = m.group(0);
    DateMatcher.add(inp1);
}
注意:遵循Java的标准命名约定。变量名不应以大写字母开头

如果没有匹配项,可以添加空字符串

if (!m.matches()) {
    DateMatcher.add("");
} else {
    String inp1 = m.group(0);
    DateMatcher.add(inp1);
}

注意:遵循Java的标准命名约定。变量名不应以大写字母开头。

我将使用以下模式进行匹配:

\b(\S+) and my date of birth is (\d{2}-\w{3}-\d{4})?
代码示例:

String input = "Hey my name is alex and my date of birth is 19-may-1999\nHey my name is raman and my date of birth is 21-may-1999\nHey my name is ali and my date of birth is \nHey my name is jack and my date of birth is 23-may-1999";
String pattern = "\\b(\\S+) and my date of birth is (\\d{2}-\\w{3}-\\d{4})?";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
    System.out.println(m.group(1) + " ; " + (m.group(2) == null ? "" : m.group(2)));
}
这张照片是:

alex ; 19-may-1999
raman ; 21-may-1999
ali ; 
jack ; 23-may-1999

我将使用以下模式进行匹配:

\b(\S+) and my date of birth is (\d{2}-\w{3}-\d{4})?
代码示例:

String input = "Hey my name is alex and my date of birth is 19-may-1999\nHey my name is raman and my date of birth is 21-may-1999\nHey my name is ali and my date of birth is \nHey my name is jack and my date of birth is 23-may-1999";
String pattern = "\\b(\\S+) and my date of birth is (\\d{2}-\\w{3}-\\d{4})?";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
    System.out.println(m.group(1) + " ; " + (m.group(2) == null ? "" : m.group(2)));
}
这张照片是:

alex ; 19-may-1999
raman ; 21-may-1999
ali ; 
jack ; 23-may-1999

请为你的文章添加一些其他关键字。请为你的文章添加一些其他关键字。我尝试了你的,但它与我的不起作用,使用这个我得到了如下输出:alex;拉曼光谱;阿里;杰克;我尝试了你的,但它与我的工作不好,使用这个我得到的输出像这样:亚历克斯;拉曼光谱;阿里;杰克;我尝试了你的,但它与我的工作不好,使用这个我得到的输出像这样:亚历克斯;拉曼光谱;阿里;杰克;我尝试了你的,但它与我的工作不好,使用这个我得到的输出像这样:亚历克斯;拉曼光谱;阿里;杰克;