匹配 bash 中以逗号分隔的 4 个字符串列表的正则表达式
我试图在 bash 中编写一个正则表达式函数,其中列表中的第一件事和第三件事是相同的,列表中的第二件事和第四件事是相同的。
grep -E "^(([0-9a-zA-Z][0-9a-zA-Z_]*)([,][0-9a-zA-Z][0-9a-zA-Z_]*)*)$"
这是我现在的正则表达式。我不确定如何检查第一和第三、第二和第四是否相同。例如,a,b,a,b 匹配,但 a,b,b,b 不匹配。
Im trying to write a regex function in bash where the first and third thing in the list are the same and the second and fourth thing in the list are the same.
grep -E "^(([0-9a-zA-Z][0-9a-zA-Z_]*)([,][0-9a-zA-Z][0-9a-zA-Z_]*)*)quot;
This is my regular expression as of now. I am unsure on how to check if the first and third, second and fourth are the same. Say for example a,b,a,b matches but a,b,b,b doesnt.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
awk
非常简单:It is quite straightforward with
awk
:使用反向引用检查一个零件是否与先前捕获的字符串匹配。
Use a back-reference to check that one part matches a previously captured string.
问题中没有任何限制“事物”是字母数字。使用此grep命令和正则表达式:
grep -e”([^,]*),([^,]*),\ 1,\ 2“
。Nothing in the question restricts "things" to be alphanumeric. Use this grep command and regular expression:
grep -E "([^,]*),([^,]*),\1,\2"
.