脚本语言-寻高手

来源:百度知道 编辑:UC知道 时间:2024/09/23 17:13:41
Question 3:
---------------------------------------------------------------------------------------

Write a regular expression to match "BALTIMORE COUNTY" in
"<p><strong>BALTIMORE COUNTY</strong></p>" and "NEW YORK"
in "<p><strong>NEW YORK</strong></p>"

---------------------------------------------------------------------------------------
Question 4:
---------------------------------------------------------------------------------------

Here's the PERL syntax of string substitution:
VARIABLE =~ s/SEARCH_PATTERN/NEW_STRING/

For instance,
$a = 'abc';
$a =~ s/a/1/; # $a is now '1bc'

Here's the question:
write a substitution statement(ie. s/SEARCH_PATTERN/NEW_STRING/) so that
"<date>1999-02-25</date>" will be updated to "<date>02-25-1999</

我就用PHP来写吧(你自己没有说用PERL还是JAVASCRIPT,不准提新要求,我可写了半天,要换语言请新发帖子:)。

第3题
程序如下:
<?php
$s='<p><strong>BALTIMORE COUNTY</strong></p>
<p><strong>NEW YORK</strong></p>';
if (preg_match_all('/<p><strong>(.*?)<\/strong><\/p>/', $s, $reg)) print_r($reg[1]);
?>
运行结果如下:
E:\ygb>php a.php
Array
(
[0] => BALTIMORE COUNTY
[1] => NEW YORK
)

第4题
程序如下:
<?php
$s='<date>1999-02-25</date>
<date>2005-11-03</date>';
$s=preg_replace('/>(\d+)-(\d+)-(\d+)</', '>$2-$3-$1<', $s);
echo $s;
?>
运行结果如下:
E:\ygb>php a.php
<date>02-25-1999</date>
<date>11-03-2005</date>

第5题
程序如下:
<?php
//程序让从文件里面读数据,PHP有函数file_get_contents可以完成,这