使用捕获组匹配重复 [01] 的正则表达式
RegEx for matching repeating [01] using capturing groups
我有一个可变长度的值字符串(实际位:1 和 0,32 的倍数)。例如:
010011011001110111100111011010001001100011101100100011100010100011110010100011001111111101101001
每个 32 位块都包含一个内部结构:前 8 位和接下来的 24 位属于一起。
我喜欢
- 获取每个 32 位块并
- 每个块的内部结构
在一个正则表达式中。
我的做法
^(([01]{8})([01]{24})){0,}$
没有成功,因为它只匹配最后一个块。
这样的正则表达式可能吗?要找什么?我做错了什么?
我使用this tool稍微修改了一下:
(([0-1]{8})([0-1]{24}))
如果我没理解错的话,你可能不想用开始和结束字符来绑定它。您可以简单地在它周围使用另一个捕获组,并与您已经拥有的其他两个捕获组一起,根据需要提取数据。
正则表达式描述图
这个link可以帮助你形象化你的表情:
JavaScript 测试演示
const regex = /(([0-1]{8})([0-1]{24}))/gm;
const str = `010011011001110111100111011010001001100011101100100011100010100011110010100011001111111101101001
`;
const subst = `Group #1: \nGroup #2: \nGroup #3: \n`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
性能测试
此代码段 returns 100 万次 for
循环的运行时间。
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const regex = /(([0-1]{8})([0-1]{24}))/gm;
const str = `010011011001110111100111011010001001100011101100100011100010100011110010100011001111111101101001`;
const subst = `\nGroup #1: \nGroup #2: \nGroup #3: `;
var match = str.replace(regex, subst);
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ");
在java中,您一次只能匹配一个。
代码
// \G matches only exactly where the previous `find()` left off
// (?:^|\G) matches either at start of line or where previous `find()` left off
Pattern p = Pattern.compile("(?:^|\G)([01]{8})([01]{24})");
// inputString should not contain e.g. newline characters
Matcher m = p.matcher(inputString);
boolean lastMatchEnd = 0;
while (m.find()) {
String firstPart = m.group(1);
String secondPart = m.group(2);
// ...
// remember how far we got
lastMatchEnd = m.end();
}
if (lastMatchEnd != inputString.length()) {
// if we get here, there were garbage in the line that did not match
}
我有一个可变长度的值字符串(实际位:1 和 0,32 的倍数)。例如:
010011011001110111100111011010001001100011101100100011100010100011110010100011001111111101101001
每个 32 位块都包含一个内部结构:前 8 位和接下来的 24 位属于一起。
我喜欢
- 获取每个 32 位块并
- 每个块的内部结构
在一个正则表达式中。
我的做法
^(([01]{8})([01]{24})){0,}$
没有成功,因为它只匹配最后一个块。
这样的正则表达式可能吗?要找什么?我做错了什么?
我使用this tool稍微修改了一下:
(([0-1]{8})([0-1]{24}))
如果我没理解错的话,你可能不想用开始和结束字符来绑定它。您可以简单地在它周围使用另一个捕获组,并与您已经拥有的其他两个捕获组一起,根据需要提取数据。
正则表达式描述图
这个link可以帮助你形象化你的表情:
JavaScript 测试演示
const regex = /(([0-1]{8})([0-1]{24}))/gm;
const str = `010011011001110111100111011010001001100011101100100011100010100011110010100011001111111101101001
`;
const subst = `Group #1: \nGroup #2: \nGroup #3: \n`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
性能测试
此代码段 returns 100 万次 for
循环的运行时间。
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const regex = /(([0-1]{8})([0-1]{24}))/gm;
const str = `010011011001110111100111011010001001100011101100100011100010100011110010100011001111111101101001`;
const subst = `\nGroup #1: \nGroup #2: \nGroup #3: `;
var match = str.replace(regex, subst);
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ");
在java中,您一次只能匹配一个。
代码
// \G matches only exactly where the previous `find()` left off
// (?:^|\G) matches either at start of line or where previous `find()` left off
Pattern p = Pattern.compile("(?:^|\G)([01]{8})([01]{24})");
// inputString should not contain e.g. newline characters
Matcher m = p.matcher(inputString);
boolean lastMatchEnd = 0;
while (m.find()) {
String firstPart = m.group(1);
String secondPart = m.group(2);
// ...
// remember how far we got
lastMatchEnd = m.end();
}
if (lastMatchEnd != inputString.length()) {
// if we get here, there were garbage in the line that did not match
}