Artificial Intelligence Programming Lab(AIPLab) 討論區

Please login or register.

請輸入帳號, 密碼以及預計登入時間

新聞:

[慶賀]恭喜張清貿醫師升任北榮傳醫科主治醫師-20170201

作者 主題: Perl regular expressions  (閱讀 1646 次)

admin

  • 管理員
  • Hero Member
  • *****
  • 文章: 1839
    • 檢視個人資料
Perl regular expressions
« 於: 十二月 06, 2016, 11:46:39 pm »
The basic method for applying a regular expression is to use the pattern binding operators =~ and !~. The first operator is a test and assignment operator.

Match Regular Expression - m//
程式碼: [Select]
$bar = "This is foo and again foo";
if ($bar =~ /foo/){
   print "First time is matching\n";
}else{
   print "First time is not matching\n";
}
已記錄

admin

  • 管理員
  • Hero Member
  • *****
  • 文章: 1839
    • 檢視個人資料
回覆: Perl regular expressions
« 回覆文章 #1 於: 十二月 06, 2016, 11:47:32 pm »
Regular Expression Variables
Regular expression variables include $, which contains whatever the last grouping match matched; $&, which contains the entire matched string; $`, which contains everything before the matched string; and $', which contains everything after the matched string.
程式碼: [Select]
$string = "The food is in the salad bar";
$string =~ m/foo/;
print "Before: $`\n";
print "Matched: $&\n";
print "After: $'\n";
已記錄

admin

  • 管理員
  • Hero Member
  • *****
  • 文章: 1839
    • 檢視個人資料
回覆: Perl regular expressions
« 回覆文章 #2 於: 十二月 06, 2016, 11:54:11 pm »
The Substitution Operator
s/PATTERN/REPLACEMENT/;
程式碼: [Select]
$string = "The cat sat on the mat";
$string =~ s/cat/dog/;

print "$string\n";
已記錄

admin

  • 管理員
  • Hero Member
  • *****
  • 文章: 1839
    • 檢視個人資料
回覆: Perl regular expressions
« 回覆文章 #3 於: 十二月 06, 2016, 11:57:13 pm »
Matching variables

程式碼: [Select]
$time = "12:05:30";

$time =~ m/(\d+):(\d+):(\d+)/;
my ($hours, $minutes, $seconds) = ($1, $2, $3);

print "Hours : $hours, Minutes: $minutes, Second: $seconds\n";
已記錄

admin

  • 管理員
  • Hero Member
  • *****
  • 文章: 1839
    • 檢視個人資料
回覆: Perl regular expressions
« 回覆文章 #4 於: 十二月 07, 2016, 12:29:26 am »
Rename
程式碼: [Select]
rename -v   's/(D\d\d\d)\S+(_R[12])\S+\.fastq$/$1$2\.fastq/' *.fastq

已記錄
 

SimplePortal Classic 2.0.5