简体   繁体   中英

Understanding Regular Expression converting to Linux from Perl script

Can someone tell me whats the definition of the two code below ?

$file_directory =~ s/[^\\\/]+$//;

$new_id =~ m/([ABCISRDT]\d\d\.\d\d\.\d\d)$/i)

It is similar to the code below but the syntax is different

$string_to_change =~ s/pattern_to_look_for/string_to_replace_with/g;

I understand s/[ is search,^ beginning of line" symbol and $ is end of line" symbol; I don't understand is \\/]+.

Let say that the file directory is W:\\folder_1\\music\\common in Perl script it should be W:\\\\folder_1\\\\music\\\\common but there is an extra one \\ in it and (I realize when I was typing it requires 3 \\ in order to display 2 \\)what does the + stands for ?If I wish to convert it into Linux should the changes be like this ?

$file_directory =~ s/[^\\//]+$//;

As for the last code I need someone to explain to me what it does Thank you .

This expression

$file_directory =~ s/[^\\\\\\/]+$//;

will match with all the characters after the last forward or backward slash, and replace it with nothing. Usually this will end up with the directory name. (Note that there are safer ways to do this, which are also cross-platform. For instance Path::Class .)

  • [^] is a negated character class, and will match anything not in the group.
  • + gives one or more matches
  • $ matches end of string

If I wish to convert it into Linux should the changes be like this ?

Since the line already checks for both slashes, the code in question should work for Linux as is.

Those square brackets delimit a character class , which means any of the chars listed. A character class the starts with ^ is a negated character class - it means anything other than those chars listed.

The expression:

[^\\\/]+$

Means "one or more of any char other than either slash at the end of input"

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