我的正则表达式太匹配了.我如何让它停止? [英] My regex is matching too much. How do I make it stop?

查看:38
本文介绍了我的正则表达式太匹配了.我如何让它停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个巨大而丑陋的字符串:

I have this gigantic ugly string:

J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
J0000010: Project name: E:\foo.pf
J0000011: Job name: MBiek Direct Mail Test
J0000020: Document 1 - Completed successfully

我正在尝试使用正则表达式从中提取片段.在这种情况下,我想抓取 Project Name 之后的所有内容,直到它显示 J0000011: 的部分(11 每次都是不同的数字).

I'm trying to extract pieces from it using regex. In this case, I want to grab everything after Project Name up to the part where it says J0000011: (the 11 is going to be a different number every time).

这是我一直在玩的正则表达式:

Here's the regex I've been playing with:

Project name:\s+(.*)\s+J[0-9]{7}:

问题是它直到最后碰到 J0000020: 才停止.

The problem is that it doesn't stop until it hits the J0000020: at the end.

如何使正则表达式在第一次出现 J[0-9]{7} 时停止?

How do I make the regex stop at the first occurrence of J[0-9]{7}?

推荐答案

通过在 .* 后面添加 '?' 使 .* 变得非贪婪:

Make .* non-greedy by adding '?' after it:

Project name:\s+(.*?)\s+J[0-9]{7}:

这篇关于我的正则表达式太匹配了.我如何让它停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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