java - How to write a regex capture group which matches a character 3 or 4 times before a delimiter?
问题描述
I'm trying to write a regex that splits elements out according to a delimiter. The regex also needs to ensure there are ideally 4, but at least 3 colons :
in each match.
Here's an example string:
"Checkers, etc:Blue::C, Backgammon, I say:Green::Pepsi:P, Chess, misc:White:Coke:Florida:A, :::U"
From this, there should be 4 matches:
Checkers, etc:Blue::C
Backgammon, I say:Green::Pepsi:P
Chess, misc:White:Coke:Florida:A
:::U
Here's what I've tried so far:
([^:]*:[^:]*){3,4}(?:, )
Regex 101 at: https://regex101.com/r/O8iacP/8
I tried setting up a non-capturing group for ,
Then I tried matching a group of any character that's not a :
, a :
, and any character that's not a :
3 or 4 times.
The code I'm using to iterate over these groups is:
String line = "Checkers, etc:Blue::C, Backgammon, I say::Pepsi:P, Chess:White:Coke:Florida:A, :::U";
String pattern = "([^:]*:[^:]*){3,4}(?:, )";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher matcher = r.matcher(line);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Any help is appreciated!
Edit
Using @Casimir's regex, it's working. I had to change the above code to use group(0) like this:
String line = "Checkers, etc:Blue::C, Backgammon, I say::Pepsi:P, Chess:White:Coke:Florida:A, :::U";
String pattern = "(?![\\s,])(?:[^:]*:){3}\\S*(?![^,])";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher matcher = r.matcher(line);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
Now prints:
Checkers, etc:Blue::C
Backgammon, I say::Pepsi:P
Chess:White:Coke:Florida:A
:::U
Thanks again!
解决方案
我建议这种模式:
(?![\\s,])(?:[^:]*:){3}\\S*(?![^,])
负前瞻避免匹配前导或尾随分隔符。第二个特别强制匹配后跟分隔符或字符串结尾(后面不跟不是逗号的字符)。
请注意,该模式没有捕获组,因此结果是整个匹配(或组 0)。
推荐阅读
- numpy - ValueError:检查输入时出错:预期 keras_layer_input 有 4 个维度,但得到了形状为 (10, 1) 的数组
- reactjs - 如何将动态数据嵌入到 JSON 字符串中
- python - 如何找到python标准库的类型?
- python - 不通过别名分配 Pydantic 字段
- javascript - 如何让 jQuery 代码在触摸屏设备上工作
- javascript - Express:提交表单时返回 ? 在网址中
- google-apps-script - Chrome 扩展和工作区插件的相同 OAuth 应用程序
- python - 编辑 Jupyter Notebooks 时在 VS Code 中缺少“在选择中查找”
- php - 长轮询中超过最大执行时间
- sql - 具有多个连接的查询索引