简体   繁体   中英

unmatched square bracket Perl regex

I am sure to have done some search, and I am not a complete novice in regexes (say, I use them regularly, not pun intended, in vim ). Let's say I have a path /home/myself/input.txt and I would like to extract input.txt . What can be easier, right. So, here is my Perl line (and I am indeed new to Perl):

my $s = "/home/myself/input.txt";
if ( $s =~ /^.*([^/]+)$/ ) { # match anything except a slash...
    my $in = "$1"; #extract the first group
}

However, the terminal is not happy:

Unmatched [ in regex; marked by <-- HERE in

What am I missing in such a simple situation? Doe square brackets have some other meaning in Perl?

Your regex is wrong, that's very simple.

The delimiter is / , so your regex should looks like :

 /^.*([^\/]+)$/
 #      ^  backslash is mandatory with / delimiter

Another solution, using m// match operator and an arbitrary ! delimiter :

m!^.*([^/]+)$!

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