比较两个文本的子字符串。

huangapple go评论63阅读模式
英文:

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 = &quot;***42*12*4**&quot;;
$input = &quot;128420120400&quot;;
$regex = str_replace(&quot;*&quot;, &quot;\d&quot;, $mask);  // \d\d\d42\d12\d4\d\d
if (preg_match(&quot;/^&quot; . $regex . &quot;$/&quot;, $input)) {
    echo &quot;MATCH&quot;;
}

huangapple
  • 本文由 发表于 2023年7月20日 16:04:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定