简体   繁体   中英

Read file using regex Perl

I have a configuration file name camera.start . The configuration looks like this :

video4linux2 -i /dev/video0 -vcodec

My question is, how can we read the /dev/video0 only in Regex? But know this that the value /dev/video0 is always changing. So i cannot read that file using [characters] modifiers. Any suggestion on how to read this configuration file using regex? Any help would be appreciated.

No need for regex, just split on a space character:

my $data = 'video4linux2 -i /dev/video0 -vcodec';

my @values = split(' ', $data);

print @values[2];
my $str = 'video4linux2 -i /dev/video0 -vcodec';

my $dev = (split ' ', $str)[2];

print $dev;

The string can be parsed by splitting on ' ' caracter.

But if you still want a regex, here you are

my $str = "video4linux2 -i /dev/video0 -vcodec";
($i) = ($str =~ m/video4linux2 -i (\S+) -vcodec/);

print "$i\n";  # '/dev/video0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM