java正则 以多个开头,Java,正则表达式HasNext以空行开头,支持多平台

该博客讨论了在Unix和Windows环境下使用Java处理文件时,如何通过正则表达式正确地分割并忽略特定的数据块。内容涉及到使用Scanner类和Multiline模式来匹配连续的非空行,以避免处理不想要的数据块。
摘要由CSDN通过智能技术生成

I need to process the following file on Unix and Windows:

a;b

c;d;e;f;g

c;d;e;f;g

c;d;e;f;g

a;b

c;d;e;f;g

c;d;e;f;g

c;d;e;f;g

a;b

a;b

c;d;e;f;g

c;d;e;f;g

c;d;e;f;g

i need to process a;b that contain a block of data underneath.

e.g. the third a;b shouldn't be processed.

currently i am delimiting by using the following regular expression this type of text in a file using Java scanner:

Scanner fileScanner = new Scanner(file);

try{

fileScanner.useDelimiter(Pattern.compile("^$", Pattern.MULTILINE));

while(fileScanner.hasNext()){

String line;

while ((line = fileScanner.nextLine()).isEmpty());

InputStream is = new ByteArrayInputStream(fileScanner.next().getBytes("UTF-8"));

...

This will still delegate for the third a;b the empty input into the ByteArrayInputStream.

Hoe may i check if the first line of fileScanner.next() is an empty line and then execute nextLine() statement and a following a continue statement?

解决方案

Use regex pattern

(?m)^(?:.+(?:\\r?\\n|\\Z)){2,}

which matches two or more non-empty lines, or other words two or more (?:...){2,} lines that contain one or more characters .+ followed by new line \\r?\\n or (?:...|...) end of string \\Z.

Multiline modifier (?m) means that ^ matches a beginning of each line, not just the beginning of the string.

Demo:

String str = "...";

Pattern p = Pattern.compile("(?m)^(?:.+(?:\\r?\\n|\\Z)){2,}");

Matcher m = p.matcher(str);

while (m.find()) {

String match = m.group();

System.out.println(match);

}

在 MongoDB 的 Java 驱动程序中,可以使用正则表达式来进行模糊匹配查询。以下是一个示例代码,展示了如何在 Java 中使用正则表达式查询 MongoDB 数据库: ```java import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import org.bson.Document; import java.util.regex.Pattern; import static com.mongodb.client.model.Filters.regex; public class MongoDBRegexExample { public static void main(String[] args) { // 连接 MongoDB 服务器 MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017"); // 选择数据库和集合 MongoDatabase database = mongoClient.getDatabase("yourDatabaseName"); MongoCollection<Document> collection = database.getCollection("yourCollectionName"); // 定义正则表达式模式 Pattern pattern = Pattern.compile("^John.*", Pattern.CASE_INSENSITIVE); // 构建查询条件,并执行查询 Document query = new Document("name", pattern); MongoCursor<Document> cursor = collection.find(query).iterator(); // 遍历结果集 while (cursor.hasNext()) { Document document = cursor.next(); System.out.println(document.toJson()); } // 关闭连接 mongoClient.close(); } } ``` 在上述示例中,我们通过 `Pattern.compile` 方法创建了一个正则表达式模式,用于匹配 `name` 字段以 "John" 开头的文档。然后,我们使用 `collection.find(query)` 方法执行查询,并使用 `iterator` 遍历结果集。 请注意,上述示例中的 `yourDatabaseName` 和 `yourCollectionName` 分别代表您的数据库名称和集合名称,请根据实际情况进行替换。另外,您可能需要根据自己的 MongoDB 配置更改连接字符串。 希望这个示例能帮助到您!如有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值