通过正则表达式检查字符串是否包含指定的字符串

Contents

输出结果

Found target


说明

regex expression “tar?get” means “r” can be lacked.


程式码


JavaScript

let value = "This is target";
if ( value.match(/tar?get/) ) {
    console.log("Found target");
}

Perl

my $value = "This is target";
if ( $value=~ m!tar?get!s ) {
    print "Found target\n";
}

PHP

<?php
$value = "This is target";
if (preg_match('/tar?get/s', $value)) {
    print "Found target\n";
}

Python

import re
value = "This is target"
if re.search( r'tar?get', value ):
   print("Found target")

Ruby

value = "This is target";
if value.match("tar?get")
  print "Found target\n";
end