简体   繁体   中英

Java Imports declaration regex

I am trying to write a pattern in Java to match against Java import declarations.

Example:

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputFormat;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
// import org.apache.hadoop.mapreduce.Something;
/* import org.apache.hadoop.something.else; */

Would match with only:

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputFormat;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

So far I have the following regex:

"[^A-Za-z0-9\\n]? *import(static|\\s)+[\\w.]*(\\*)?(\\s)*;"

But it's not working. For example:

import org.junit.Test; 
import java.util.ArrayList;
/* The import name; lazily initialized; defaults to a unspecified,...

returns:

import org.junit.Test; 
import java.util.ArrayList; 
import name;

which is wrong.

这个怎么样:

^import

I got it working with the use of a flag.

Now it looks like the following:

Pattern.compile("(;|^ *)import(static|\\s)+[\\w.]*(\\*)?(\\s)*;",Pattern.MULTILINE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM