第一位数字与四位数字中的最后一位数字相匹配
The first digit matches the last digit in a four-digits number
我正在尝试查找以相同数字开头和结尾并且两个数字之间具有相似数字的数字。以下是一些示例:
7007 1551 3993 5115 9889
我尝试了以下正则表达式来识别第一个和最后一个数字。但是,没有选择任何号码。
^(\d{1})$
感谢您的帮助。
也许,
^(\d)(\d)+$
可能是一个值得研究的选项。
RegEx Demo
如果你想simplify/update/explore这个表达式,在regex101.com. You can watch the matching steps or modify them in this debugger link, if you'd be interested. The debugger demonstrates that how a RegEx engine的右上面板已经解释过可能会逐步消耗一些示例输入字符串并执行匹配过程。
您的正则表达式将匹配两个数字相同的数字。你只需要展开它:(\d)(\d)
另外,由于数字在同一行,因此使用字边界 (\b
) 而不是行边界 (^
和 $
)。
\b(\d)(\d)\b
顺便说一句 {1}
是多余的
Demo on regex101
使用这个:
(\d)(\d)+
分别捕获第一位和第二位数字,然后以相反的顺序匹配它们。
简单的 JS 方式。
let a = "7007 1551 3393 5115 9883";
a = a.split(" ");
let ans = [];
a.forEach((val) => {
let temp = val.split("");
if (temp && temp[0] === temp[temp.length - 1]) {
temp = temp.slice(1,temp.length-1);
ans.push(temp.slice(0,temp.length).every( (val, i, arr) => val === arr[0] )) ;
} else {
ans.push(false);
}
});
console.log(ans);
正则表达式:
let a = "7007 1551 3393 5115 9883";
a = a.split(" ");
let ans = [];
a.forEach((val) => {
let reg = /(\d)(\d*)(\d)/gi;
let match = reg.exec(val);
if (match && match.length > 3 && match[1] === match[3]) {
let temp = match[2];
temp = temp.split("");
temp = temp.slice(0,temp.length);
ans.push(temp.every( (val, i, arr) => val === arr[0] )) ;
} else {
ans.push(false);
}
});
console.log(ans);
我正在尝试查找以相同数字开头和结尾并且两个数字之间具有相似数字的数字。以下是一些示例:
7007 1551 3993 5115 9889
我尝试了以下正则表达式来识别第一个和最后一个数字。但是,没有选择任何号码。
^(\d{1})$
感谢您的帮助。
也许,
^(\d)(\d)+$
可能是一个值得研究的选项。
RegEx Demo
如果你想simplify/update/explore这个表达式,在regex101.com. You can watch the matching steps or modify them in this debugger link, if you'd be interested. The debugger demonstrates that how a RegEx engine的右上面板已经解释过可能会逐步消耗一些示例输入字符串并执行匹配过程。
您的正则表达式将匹配两个数字相同的数字。你只需要展开它:(\d)(\d)
另外,由于数字在同一行,因此使用字边界 (\b
) 而不是行边界 (^
和 $
)。
\b(\d)(\d)\b
顺便说一句 {1}
是多余的
Demo on regex101
使用这个:
(\d)(\d)+
分别捕获第一位和第二位数字,然后以相反的顺序匹配它们。
简单的 JS 方式。
let a = "7007 1551 3393 5115 9883";
a = a.split(" ");
let ans = [];
a.forEach((val) => {
let temp = val.split("");
if (temp && temp[0] === temp[temp.length - 1]) {
temp = temp.slice(1,temp.length-1);
ans.push(temp.slice(0,temp.length).every( (val, i, arr) => val === arr[0] )) ;
} else {
ans.push(false);
}
});
console.log(ans);
正则表达式:
let a = "7007 1551 3393 5115 9883";
a = a.split(" ");
let ans = [];
a.forEach((val) => {
let reg = /(\d)(\d*)(\d)/gi;
let match = reg.exec(val);
if (match && match.length > 3 && match[1] === match[3]) {
let temp = match[2];
temp = temp.split("");
temp = temp.slice(0,temp.length);
ans.push(temp.every( (val, i, arr) => val === arr[0] )) ;
} else {
ans.push(false);
}
});
console.log(ans);