正则表达式并忽略空格

发布于 2024-11-03 04:20:59 字数 397 浏览 1 评论 0原文

我有以下正则表达式,用于在文件字符串搜索中匹配各种信用卡号。但是,如果要匹配的模式之前或之后有空格,则匹配将失败。

$CC_Regex = "^(\d{4}-){3}\d{4}$|^(\d{4} ){3}\d{4}$|^\d{16}$"

例如,它将匹配前三名,但不会匹配后三名。

1111-2323-2312-3434
1234343425262837
1111 2323 2312 3434

1111-2323-2312-3434 
 1234343425262837
 1111 2323 2312 3434 

在下面的三个中,第一个的末尾有一个空格,第二个的前面有一个空格,第三个的前后都有一个空格。

预先感谢您的帮助。

I have the following regex that I am using to match on various credit card numbers within a string search of files. However, if there is a space before or after the pattern to match, then the match will fail.

$CC_Regex = "^(\d{4}-){3}\d{4}$|^(\d{4} ){3}\d{4}$|^\d{16}$"

For example, it will match the top three, but will not match the bottom three.

1111-2323-2312-3434
1234343425262837
1111 2323 2312 3434

1111-2323-2312-3434 
 1234343425262837
 1111 2323 2312 3434 

Out of the bottom three, the first one has a space at the end of it, the second one has a space before it, and the third one has a space before and after it.

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白芷 2024-11-10 04:20:59

增强正则表达式来处理前端和末尾的空格是很容易的:

^\s*(?:(\d{4}-){3}\d{4}|(\d{4} ){3}\d{4}|\d{16})\s*$

但更谨慎的做法是获取字符串,删除除数字之外的所有内容,然后检查它是否是 16 位数字:

$result = $subject -creplace '[^0-9]+', ''
$found = $result -cmatch '^[0-9]{16}

毕竟,谁知道是什么人们可能对格式有一些想法 - 组之间有几个空格,空格和破折号的混合等等......

毕竟,谁知道是什么人们可能对格式有一些想法 - 组之间有几个空格,空格和破折号的混合等等......

It would be easy enough to augment the regex to handle whitespace at the front and end:

^\s*(?:(\d{4}-){3}\d{4}|(\d{4} ){3}\d{4}|\d{16})\s*$

but it seems more prudent to take the string, remove everything except digits, and then check if it's a 16-digit number:

$result = $subject -creplace '[^0-9]+', ''
$found = $result -cmatch '^[0-9]{16}

After all, who knows what ideas people might have for formatting - several spaces between groups, mixes of spaces and dashes etc...

After all, who knows what ideas people might have for formatting - several spaces between groups, mixes of spaces and dashes etc...

明媚如初 2024-11-10 04:20:59

我不知道如何在 PowerShell 中具体执行此操作,但使用某种 Replace() 函数来删除空格(或 PowerShell 中的等效函数)会容易得多。

replace(' ', '');

编辑:实际上,首先删除所有内容会更有意义。您可以使用 RegEx 轻松做到这一点:

[^0-9]+

I don't know how specifically to do this in PowerShell, but it'd be a lot easier to use some sort of replace() function to get rid of the spaces (or the equivalent in PowerShell).

replace(' ', '');

EDIT: Actually, it'd make more sense to remove everything that's not a digit first. You could do that with RegEx easily:

[^0-9]+
じ违心 2024-11-10 04:20:59
$CC_Regex = "^\s*(\d{4}-){3}\d{4}\s*$|^\s*(\d{4} ){3}\d{4}\s*$|^\s*\d{16}\s*$"

即使存在前导或尾随空白字符(\s 表示任何空白字符),也会使其匹配。

$CC_Regex = "^\s*(\d{4}-){3}\d{4}\s*$|^\s*(\d{4} ){3}\d{4}\s*$|^\s*\d{16}\s*$"

will make it match even if leading or trailing whitepsace is present (\s means any whitespace character).

作死小能手 2024-11-10 04:20:59
$CC_Regex = "(?:\d{4}[- ]?){3}\d{4}"
$CC_Regex = "(?:\d{4}[- ]?){3}\d{4}"
奶茶白久 2024-11-10 04:20:59

这个 ^(?\s{0,}\d{4}-{0,}\s{0,}\d{4}-{0,}\s{0, }\d{4}-{0,}\s{0,}\d{4}).*?

How about this ^(?<myregex>\s{0,}\d{4}-{0,}\s{0,}\d{4}-{0,}\s{0,}\d{4}-{0,}\s{0,}\d{4}).*?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文