Regex and ignore whitespace

允我心安 提交于 2019-12-11 08:13:27

问题


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.


回答1:


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




回答2:


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



回答3:


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




回答4:


$CC_Regex = "(?:\d{4}[- ]?){3}\d{4}"


来源:https://stackoverflow.com/questions/5782920/regex-and-ignore-whitespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!