处理搜索字符串中的元字符
我有一个用户输入,将在可能包含元字符的搜索字符串中使用,
对于例如 C# 或 C++,
我在函数中的 grep 命令是:
grep -E "$1|$2" test.txt
在直接替换下:
grep -E "C\+\+|testWord" test.txt
grep -E "C\#|testWord" test.txt
第一个捕获行很好,但不是第二个。 奇怪的是,# 被完全忽略了。 如果没有直接替换,两者都会用 c 后跟 testWord 而不是 c++ 和 c# 分别捕获任何内容
我尝试使用 sed 处理它
$temp = `echo $1 | sed 's/[\#\!\&\;\`\"\'\|\*\?\~\<\>\^\(\)\[\]\{\}\$\+\\]/\\&/g'`
,但它无法正常工作。 或者还有其他方法来处理带有元字符的用户输入吗?
提前致谢
I have a user input that would be used in a search string that may contain a metacharacter
For e.g. C# or C++
my grep command in a function was:
grep -E "$1|$2" test.txt
under direct replacement:
grep -E "C\+\+|testWord" test.txt
grep -E "C\#|testWord" test.txt
the first caught the lines fine but not the second.
Strangely, # was completely ignored.
Without direct replacement, both catch anything with c followed by testWord instead of c++ and c# respectively
I've tried handling it using sed
$temp = `echo $1 | sed 's/[\#\!\&\;\`\"\'\|\*\?\~\<\>\^\(\)\[\]\{\}\$\+\\]/\\&/g'`
but it doesn't work right.
Or is there any other way to handle user input with metacharacters?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您将输入作为参数传递给脚本
“
输出
如果您从 read 获取输入
if you are passing the input as arguments to the script
"
output
if you are getting input from read
这对我有用:
编辑:
您可以在不使用
-E
的情况下尝试此表单:This works for me:
Edit:
You might try this form without
-E
:只需引用 $1 和 $2 中的所有 grep 元字符,然后将它们添加到 grep 表达式中即可。
像这样的事情:
应该有效。调整元字符列表以适应。处理|有点棘手,因为反斜杠使它变得特别,但由于我们已经反斜杠反斜杠我认为它是安全的。
Just quote all the grep metacharacters in $1 and $2 before adding them to your grep expression.
Something like this:
ought to work. Adjust the metachar list to suit. Handling | is a little tricky because backslashing makes it special, but since we're already backslashing backslashes I think it's safe.