in reply to Extracting text from a string using regex
Note that \r should generally be used for cr rather than \x0d.
The following uses split to break the string into candidate sub strings, then uses a regex with a code evaluation expression in it to extract the text you want. A more conventional alternative is given too.
use strict; use warnings; my $str = "aaa authentication login\r\n ssid Rich\r\n authentication o +pen\r\n authentication shared \r\n"; my $authentication = ''; my @candidates = split /[\r\n] +/, $str; /^authentication\s+(\w*)(?{$authentication.= $1 . ' '})/ for @candidat +es; print $authentication;
Alternate extract code:
for (@candidates) { $authentication.= $1 . ' ' if /^authentication\s+(\w*)/; }
Either prints:
open shared
|
|---|