Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单Java正则表达式_Java_Regex - Fatal编程技术网

简单Java正则表达式

简单Java正则表达式,java,regex,Java,Regex,我有很多: FooModel f = new FooModel(); .. Bar Model b = new BarModel(); 我需要从Java源代码中检索任何模型对象,但我不想完成声明。我只需要对象声明 我尝试使用正则表达式(其中strLine是InputStreamReader中的字符串行): 我能得到实例。但是我会做同样的事情,但是对于对象声明(在“=”之前) 我该怎么做 我能做到 m.group(0).replace(m.group(1),""); 但它不是一个真正的正则表达

我有很多:

FooModel f = new FooModel();
..
Bar Model b = new BarModel();
我需要从Java源代码中检索任何模型对象,但我不想完成声明。我只需要对象声明

我尝试使用正则表达式(其中strLine是InputStreamReader中的字符串行):

我能得到实例。但是我会做同样的事情,但是对于对象声明(在“=”之前)

我该怎么做

我能做到

m.group(0).replace(m.group(1),"");

但它不是一个真正的正则表达式。

如果一行中有一个初始化语句:

(\\w+ \\w+)\\s*=.*new.*Model.*
import java.util.regex.*;

class  FindModel
{
    public static void main(String[] args) 
    {
        String s = "  FooModel f = new FooModel();";
        String pattern = "([^\\s]*?Model[^=]*)=";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(s);

        if(m.find())
            System.out.print(m.group(1) + "\n");
     }
}
import java.util.regex.*;

class  FindModel
{
    public static void main(String[] args) 
    {
        String s = "  FooModel f = new FooModel();int i=0,j; BarModel b = new BarModel();";
        String pattern = "([^;\\s]*?Model[^=]*)=.*?;";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(s);

        while(m.find())
            System.out.print(m.group(1) + "\n");
     }
}
如果一行中有多条语句:

import java.util.regex.*;

class  FindModel
{
    public static void main(String[] args) 
    {
        String s = "  FooModel f = new FooModel();";
        String pattern = "([^\\s]*?Model[^=]*)=";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(s);

        if(m.find())
            System.out.print(m.group(1) + "\n");
     }
}
import java.util.regex.*;

class  FindModel
{
    public static void main(String[] args) 
    {
        String s = "  FooModel f = new FooModel();int i=0,j; BarModel b = new BarModel();";
        String pattern = "([^;\\s]*?Model[^=]*)=.*?;";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(s);

        while(m.find())
            System.out.print(m.group(1) + "\n");
     }
}
输出:

FooModel f
BarModel b

这永远不会可靠地工作。如果希望得到完全正确的结果,则需要使用适当的Java解析器。下面的代码有注释:
FooModel f=newfoomodel();/*创建一个新的FooModel()*/
?是的,我知道它不起作用,但如何才能做到?m.group(0)而不是m.group(1)实际上group(0)将显示完整的FooModel f=new FooModel();String pattern=“(.*Model[^=]*)=”怎么样;系统输出打印(m.group(1)+“\n”);