开发者

Regex and ignore whitespace

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 a开发者_开发知识库fter it.

Thanks in advance for your help.


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...


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]+


$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).


$CC_Regex = "(?:\d{4}[- ]?){3}\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}).*?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜