2013-05-23 4 views
0

Я ищу для извлечения определенного набора символов, соответствующих определенному слову, до последнего пробела, которое происходит в последовательности.соответствуют последовательности между словом и последним пространством вхождения

Пример:

FAILED on portal HTTP (10.1.1.1) 
FAILED on portal TELNET 0 SSH (10.1.1.1) 

Я хочу O/P будет:

HTTP 
TELNET 0 SSH 

В настоящее время с помощью следующей Regex и работать над ней:

.+((?<=portal)[^\s]]+ 

Будет полезно, если любой из вас может помочь мне в этом :)

Обновлено от комментариев:

Текст:

1368028793000 10.3.1.4 CISCO X AUTHENTICATION:SESSION User authentication attempt FAILED on portal TELNET 0 SSH (10.1.2.8:64940) 

Regex:

^(\d+).* (\S+\d) ([\w\s]+) (\w* ?AUTHENTICATION:SESSION) (.+) (([\w.]+):(\d+)).* 

Обычно группы я хотел бы иметь от моей строки выборки будет:

#1 - 1368028793000 
#2 - 10.3.1.4 
#3 - CISCO X 
#4 - AUTHENTICATION:SESSION 
#5 - User authentication attempt FAILED on portal 
#6 - TELNET 0 SSH 
#7 - 10.1.2.8 
#8 - 6940 
+0

Ok .. Я буду описывать все это, что я пытаюсь – Designerztouch

+0

текст: 1368028793000 10.3.1.4 CISCO X AUTHENTICATION: SESSION попытки аутентификации пользователя НЕСОСТОЯВШЕЙСЯ на портал TELNET 0 SSH (10.1.2.8:64940) Regex:^(\ d +). * (\ S + \ d) ([\ w \ s] +) (\ w *? АУТЕНТИФИКАЦИЯ: СЕССИЯ) (. +) \ (([\ w.] +): (\ d +) \).* – Designerztouch

+0

Вы действительно хотите захватить все эти группы? или только то, что есть после портала? – Toto

ответ

0

Все изменения в соответствии с новыми требованиями.

У попробовать с:

^(\d+)\s+([\d.]+)\s+([\w\s]+?)\s+(AUTHENTICATION:SESSION)\s+(.+?portal)\s(.+?)\(([\d.]+)(?::(\d+))?\)$ 

Вот Perl-скрипт, который запускает его:

my $re = qr/^(\d+)\s+([\d.]+)\s+([\w\s]+?)\s+(AUTHENTICATION:SESSION)\s+(.+?portal)\s(.+?)\(([\d.]+)(?::(\d+))?\)$/; 
while(<DATA>) { 
    chomp; 
    my @l = ($_ =~ $re); 
    [email protected]; 
} 
__DATA__ 
1368028793000 10.3.1.4 CISCO X AUTHENTICATION:SESSION User authentication attempt FAILED on portal HTTP (10.1.1.1) 
1368028793000 10.3.1.4 CISCO X AUTHENTICATION:SESSION User authentication attempt FAILED on portal TELNET 0 SSH (10.1.2.8:64940) 

выход:

(
    1368028793000, 
    "10.3.1.4", 
    "CISCO X", 
    "AUTHENTICATION:SESSION", 
    "User authentication attempt FAILED on portal", 
    "HTTP ", 
    "10.1.1.1", 
    undef, 
) 
(
    1368028793000, 
    "10.3.1.4", 
    "CISCO X", 
    "AUTHENTICATION:SESSION", 
    "User authentication attempt FAILED on portal", 
    "TELNET 0 SSH ", 
    "10.1.2.8", 
    64940, 
) 

регулярное выражение объяснить:

The regular expression: 

(?-imsx:^(\d+)\s+([\d.]+)\s+([\w\s]+?)\s+(AUTHENTICATION:SESSION)\s+(.+?portal)\s(.+?)\(([\d.]+)(?::(\d+))?\)$) 

matches as follows: 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
(?-imsx:     group, but do not capture (case-sensitive) 
         (with^and $ matching normally) (with . not 
         matching \n) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
^      the beginning of the string 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    \d+      digits (0-9) (1 or more times (matching 
          the most amount possible)) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    (      group and capture to \2: 
---------------------------------------------------------------------- 
    [\d.]+     any character of: digits (0-9), '.' (1 
          or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)      end of \2 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    (      group and capture to \3: 
---------------------------------------------------------------------- 
    [\w\s]+?     any character of: word characters (a-z, 
          A-Z, 0-9, _), whitespace (\n, \r, \t, 
          \f, and " ") (1 or more times (matching 
          the least amount possible)) 
---------------------------------------------------------------------- 
)      end of \3 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    (      group and capture to \4: 
---------------------------------------------------------------------- 
    AUTHENTICATION:SES  'AUTHENTICATION:SESSION' 
    SION 
---------------------------------------------------------------------- 
)      end of \4 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    (      group and capture to \5: 
---------------------------------------------------------------------- 
    .+?      any character except \n (1 or more times 
          (matching the least amount possible)) 
---------------------------------------------------------------------- 
    portal     'portal' 
---------------------------------------------------------------------- 
)      end of \5 
---------------------------------------------------------------------- 
    \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
    (      group and capture to \6: 
---------------------------------------------------------------------- 
    .+?      any character except \n (1 or more times 
          (matching the least amount possible)) 
---------------------------------------------------------------------- 
)      end of \6 
---------------------------------------------------------------------- 
    \(      '(' 
---------------------------------------------------------------------- 
    (      group and capture to \7: 
---------------------------------------------------------------------- 
    [\d.]+     any character of: digits (0-9), '.' (1 
          or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)      end of \7 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (optional 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    :      ':' 
---------------------------------------------------------------------- 
    (      group and capture to \8: 
---------------------------------------------------------------------- 
     \d+      digits (0-9) (1 or more times 
           (matching the most amount possible)) 
---------------------------------------------------------------------- 
    )      end of \8 
---------------------------------------------------------------------- 
)?      end of grouping 
---------------------------------------------------------------------- 
    \)      ')' 
---------------------------------------------------------------------- 
    $      before an optional \n, and the end of the 
          string 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
+0

Спасибо .. Но это одна дает ниже O/P портал TELNET 0 SSH (а) TELNET 0 SSH – Designerztouch

+0

@Designerztouch: жаль, что я не понимаю вас. – Toto

+0

Хорошо. Я поставил именно то, что мне нужно от чего :) Надеюсь, теперь это ясно. Благодаря! – Designerztouch

0

Вы можете использовать это регулярное выражение

(?<=portal).+(?=\s) 

.+ жаден, так что будет соответствовать до конца, а затем при необходимости вернуться назад ..

1

Вы могли бы, возможно, попробовать это:

(?<=portal\s)(.+)\s\(

Обратите внимание, что у вас есть недостающие закрывающая скобка ) и недостающее открытие квадратных скобок [, который я предполагаю, была опечаткой. И вам нужно избегать открывающей скобки, которая знаменует начало бит (10.1.1.1).