正则表达式:
/(?!\s)([^]+?)\s*((?!.+\)\s*\()\([^-].+ \d{2}\/\d{2}\/\d{4} .+\))/g
输入:
Some notes(SOME USER (SU950) 16/09/2015 16:56:38)Some other notes(ANOTHER > USER (AU951) 16/09/2015 16:56:38)
Example Notes Type:OUTBOUND CALL BACK (USER NAME (UN973) 18/09/2015 11:49:10)
some notes on multiple lines
second line of notes (USER NAME (UN973) 18/09/2015 11:52:24)
A new note. (USER NAME (UN973) 18/09/2015 11:55:15)
预期输出:
- 一些笔记(一些用户(SU950)16/09/2015 16:56:38)
- 一些笔记
- (一些用户(SU950)16/09/2015 16:56:38)
- 一些其他注意事项(另一位 > 用户 (AU951) 16/09/2015 16:56:38)
- 一些其他注意事项
- (另一位 > 用户 (AU951) 16/09/2015 16:56:38)
- 示例备注类型:OUTBOUND CALL BACK (USER NAME (UN973) 18/09/2015 11:49:10)
- 示例备注类型:OUTBOUND CALL BACK
- (用户名(UN973)18/09/2015 11:49:10)
- 一些多行注释第二行注释(用户名(UN973)18/09/2015 11:52:24)
- 多行注释第二行注释
- (用户名(UN973)18/09/2015 11:52:24)
- 一个新笔记。 (用户名 (UN973) 18/09/2015 11:55:15)
- 一条新笔记。
- (用户名(UN973)18/09/2015 11:55:15)
实际输出:
- 一些笔记(SOME USER (SU950) 16/09/2015 16:56:38)一些其他笔记(ANOTHER > USER (AU951) 16/09/2015 16:56:38)
- 一些笔记
- (某位用户 (SU950) 16/09/2015 16:56:38)一些其他注意事项(另一位用户 (AU951) 16/09/2015 16:56:38)
- 示例备注类型:OUTBOUND CALL BACK (USER NAME (UN973) 18/09/2015 11:49:10)
- 示例备注类型:OUTBOUND CALL BACK
- (用户名(UN973)18/09/2015 11:49:10)
- 一些多行注释第二行注释(用户名(UN973)18/09/2015 11:52:24)
- 多行注释第二行注释
- (用户名(UN973)18/09/2015 11:52:24)
- 一个新笔记。 (用户名 (UN973) 18/09/2015 11:55:15)
- 一条新笔记。
- (用户名(UN973)18/09/2015 11:55:15)
正则表达式:
http://regexr.com/3d79o
我认为问题是当涉及到表达式的最后部分时,我没有匹配尽可能少的出现次数,但是,我没有成功使用 +?运营商。
最佳答案
一个简单的方法是使用下面的 regex
,(如果格式是固定的)
([^(]*)(\(.*?\(?.*?\).*?\))
Javascript 代码
var re = /([^(]*)(\(.*?\(?.*?\).*?\))/g;
var str = 'Some notes(SOME USER (SU950) 16/09/2015 16:56:38)Some other notes(ANOTHER > USER (AU951) 16/09/2015 16:56:38)\n\nExample Notes Type:OUTBOUND CALL BACK (USER NAME (UN973) 18/09/2015 11:49:10)\n\nsome notes on multiple lines\n\nsecond line of notes (USER NAME (UN973) 18/09/2015 11:52:24)\n\nA new note. (USER NAME (UN973) 18/09/2015 11:55:15)';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
print(m[0]);
print(m[1]);
print(m[2]);
}
关于javascript - 正则表达式 - 匹配尽可能少的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36612947/