使用 findstr 和正则表达式来搜索 CSV
我想知道是否可以使用 findstr 在 CSV 中搜索与此正则表达式匹配的任何内容
^([BPXT][0-9]{6})|([a-zA-Z][a-zA-z][0-9][0-9](adm)?)$
I was wondering if it's possible to use findstr to search through a CSV for anything matching this regular expression
^([BPXT][0-9]{6})|([a-zA-Z][a-zA-z][0-9][0-9](adm)?)$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你在谈论哪种语言,但你的正则表达式有一个明显的问题:
^
和$
锚点要求它匹配整个字符串,并且您似乎计划匹配 CSV 文件中的各个条目。因此,如果您的正则表达式引擎支持单词边界锚,则应该使用单词边界锚:
我还在交替周围添加了另一个非捕获组。在您的正则表达式中,字符串开头和结尾的锚点将是交替的一部分,这可能不是有意的。您是否真的需要所有其他括号取决于您要如何处理匹配。
I don't know which language you're talking about, but there is one obvious problem with your regex: The
^
and$
anchors require that it matches the entire string, and you seem to be planning on matching individual entries in your CSV file.Therefore, you should use word boundary anchors instead if your regex engine supports them:
I've also added another non-capturing group around the alternation. In your regex the anchors at the start and end of the string would have been part of the alternation, which is probably not intended. Whether you really need all the other parentheses depends on what you're going to do with the match.
不,无法使用
findstr
搜索匹配的子字符串,尤其是那些与您提供的复杂表达式匹配的子字符串。findstr
是 Windows 内置函数。findstr /?
显示它可以使用的正则表达式子集:这意味着你的大部分表达都在窗外。
此外,findstr 不能将其输出限制为仅匹配的表达式;它仅识别包含匹配项的行。
它完全不适合所描述的任务。
No, it is not possible to use
findstr
to search for matching substrings, especially those matching the complex expression you've provided.findstr
is a Windows built-in.findstr /?
shows the subset of regex that it can use:This means that most of your expression is out the window.
Also,
findstr
can't limit its output to just the matched expression; it only identifies lines containing matches.It is entirely unsuitable for the task described.