英文:
compare substring of two text
问题
['42124*' 和 '128420120400' 代码相同]
英文:
I have two arrays like below
['*01***12*4**','0*****54*41*','***18*******','*2*7**3***1','***42*12*4**',...]
['128420120400','012189654700','321501481012','88114474111',...]
I want to compare the strings of two arrays without considering the stars and put the same codes in an array (the length of the codes in both arrays is 12)
In the example above, '***42*12*4**'
and '128420120400
'codes are equal.
I use php (laravel)
答案1
得分: 6
一种方法是使用正则表达式,其中遮罩字符串中的每个 *
占位符都被替换为 \d
,后者表示一个数字。
$mask = "***42*12*4**";
$input = "128420120400";
$regex = str_replace("*", "\d", $mask); // \d\d\d42\d12\d4\d\d
if (preg_match("/^" . $regex . "$/", $input)) {
echo "MATCH";
}
英文:
One approach would be to use regular expressions, where each *
placeholder in the masked string is replaced by \d
, the latter which represents a single digit.
<!-- language: php -->
$mask = "***42*12*4**";
$input = "128420120400";
$regex = str_replace("*", "\d", $mask); // \d\d\d42\d12\d4\d\d
if (preg_match("/^" . $regex . "$/", $input)) {
echo "MATCH";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论