gpt4 book ai didi

java - 正则表达式来查看字符串是否包含模式

转载 作者:行者123 更新时间:2023-12-01 06:19:54 25 4
gpt4 key购买 nike

我是正则表达式的新手,我发现完成这个简单的任务非常困难。整个字符串看起来像

debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: read PEM private key done: type RSA
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (publickey).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending subsystem: sftp
Can't ls: "/home/dev/customer/*.out" not found
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 2 clearing O_NONBLOCK
debug1: Transferred: stdin 0, stdout 0, stderr 0 bytes in 0.1 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 0.0
debug1: Exit status 0
debug1: compress outgoing: raw data 343, compressed 184, factor 0.54
debug1: compress incoming: raw data 860, compressed 430, factor 0.50

我只需要检查此字符串中是否存在诸如 *.out/text"not find 之类的内容。在这种情况下它确实存在,但我没有得到正确的输出

我使用的正则表达式是 .*\*\..*"not found.* 不起作用。有人可以帮忙吗?

最佳答案

使用字符串匹配

您实际上并不需要为此使用正则表达式。一个简单的字符串匹配就可以很好地工作,而且速度也更快!例如:

$ fgrep '.out" not found' /tmp/foo
Can't ls: "/home/dev/customer/*.out" not found

如果你真的想要正则表达式

如果您坚持的话,可以使用正则表达式,但它不会更好地适合您的用例。不过,这里是:

$ egrep '\.out" not found' /tmp/foo
Can't ls: "/home/dev/customer/*.out" not found

正则表达式的技巧是确保只匹配最少的必要文本以确保正确匹配,而不是尝试使用正则表达式标记构建整行。在这种情况下,简单更好。

关于java - 正则表达式来查看字符串是否包含模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12019299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com