1

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

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 found这个字符串中是否存在类似的东西。在这种情况下它确实存在,但我没有得到正确的输出

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

4

3 回答 3

0

使用字符串匹配

您实际上并不需要正则表达式。一个简单的字符串匹配就可以了,而且速度也更快!例如:

$ 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

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

于 2012-08-18T14:25:49.010 回答
0
import java.util.regex.*;

class Main
{
  public static void main (String[] args) throws java.lang.Exception
  {
    java.io.BufferedReader in = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
    String input = "";
    String s;
    while ((s = in.readLine()) != null && s.length() != 0) input += s;
    Pattern pattern = Pattern.compile("\\*\\.(?:out|text)\\\"\\s+not\\sfound");
    Matcher  matcher = pattern.matcher(input);
    if (matcher.find())
      System.out.println("Match found");
  }
}

在此处测试此代码。

于 2012-08-18T15:23:43.370 回答
-1

这适用于多行和单行模式

(^.*?|.*?)\.(out|text).*?not found(.*?|.*?$)
于 2012-08-18T14:41:06.813 回答