REGEX多行 - powershell

时间:2015-08-10 07:13:47

标签: regex powershell

我在powershell中使用正则表达式有点问题。我的REGEX仅适用于一行。我需要多行工作。

例如html:

<li> test </li>
</ul>

我希望REGEX采取一切措施,包括“/ ul&gt;”。我的建议是:

'(^.*<li>.*</ul>)' 

但它没有用。它甚至可能吗?感谢。

2 个答案:

答案 0 :(得分:6)

取决于您正在使用的正则表达式方法。

如果您使用.NET Please Create a notepade++ file named it dbconnection.php and copy this code <?php mysql_connect('localhost','root',''); mysql_select_db('database_name'); ?> ---------------------------------------------------------- Please Create a notepade++ file named it index.php and copy this code <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>Your Title</title> <meta name = "viewport" content = "width=device-width,initial-scale=1" /> <link rel="shortcut icon" type = "image/jpg" href="images.jpg" /> <link rel="stylesheet" type = "text/css" href = "css/style.css" /> <link rel="stylesheet" type = "text/css" href = "bootstrap/css/bootstrap.css" /> <link rel="stylesheet" type = "text/css" href = "bootstrap/css/bootstrap.min.css" /> </head> <body> <div id = "frmNC_content"> <div id = "frmNC_header"> <div id = "frmNC_headertext">Find Contact Form</div> </div> <div id = "frmNC_body"> <table width = "100%" border = "0" style = "margin-top:20px;"> <form name = "frmEditContact" action = "" method = "post"> <tr> <th width = "15%" style = "text-align:center;">Name : </th> <th width = "30%"><input type = "text" name = "txtName" class = "form-control imput-sm"/></th> <th width = "15%" style = "text-align:center;">Mobile No : </th> <th width = "30%"><input type = "text" name = "txtMobileNo" class = "form-control imput-sm"/></th> <th width = "10%" style = "text-align:center;"><input type = "submit" name = "btnSearch" value = "Search" class = "btn btn-info"/></th> </tr> </form> </table> <table width = "100%" border = "1" style = "border-collapse:collapse;margin-top:20px;"> <tr height = "35px"> <th width = "20%" style = "padding-left:10px;background-color:#6495ED;">Name</th> <th width = "20%" style = "padding-left:10px;background-color:#6495ED;">Address</th> <th width = "10%" style = "padding-left:10px;background-color:#6495ED;">Mobile No</th> <th width = "20%" style = "padding-left:10px;background-color:#6495ED;">Office Name</th> <th width = "10%" style = "padding-left:10px;background-color:#6495ED;">Office No</th> <th width = "10%" style = "padding-left:10px;background-color:#6495ED;">Other No</th> <th width = "10%" style = "padding-left:10px;background-color:#6495ED;">Status</th> </tr> <?php if(isset($_POST['btnSearch'])) { if(isset($_POST['txtName']) && $_POST['txtMobileNo']=="") { $name = mysql_real_escape_string($_POST['txtName']); require('dbconnection.php'); $sql = "SELECT * FROM `tbl_new_contact` WHERE name = '$name' AND user_code = '$_SESSION[user_code]'"; $query = mysql_query($sql); while($row = mysql_fetch_array($query)): echo "<tr height = '25px'>"; echo "<td style = 'padding-left:10px;'>".$row['name']."</td>"; echo "<td style = 'padding-left:10px;'>".$row['address']."</td>"; echo "<td style = 'padding-left:10px;'>".$row['mob_no']."</td>"; echo "<td style = 'padding-left:10px;'>".$row['office_name']."</td>"; echo "<td style = 'padding-left:10px;'><a href = 'tel:$row[office_number]'>".$row['office_number']."</a></td>"; echo "<td style = 'padding-left:10px;'><a href = 'tel:$row[other_mob_no]'>".$row['other_mob_no']."</a></td>"; echo "<td style = 'padding-left:10px;'><a href = 'tel:$row[mob_no]'> <button class = 'btn btn-info'><span class='glyphicon glyphicon-phone'></span></button> </a></td>"; echo "</tr>"; endwhile; } else if(isset($_POST['txtMobileNo']) && $_POST['txtName']=="") { $mobileNo = mysql_real_escape_string($_POST['txtMobileNo']); require('dbconnection.php'); $sql = "SELECT * FROM `tbl_new_contact` WHERE mob_no = '$mobileNo' AND user_code = '$_SESSION[user_code]'"; $query = mysql_query($sql); while($row = mysql_fetch_array($query)): echo "<tr height = '25px'>"; echo "<td style = 'padding-left:10px;'>".$row['name']."</td>"; echo "<td style = 'padding-left:10px;'>".$row['address']."</td>"; echo "<td style = 'padding-left:10px;'>".$row['mob_no']."</td>"; echo "<td style = 'padding-left:10px;'>".$row['office_name']."</td>"; echo "<td style = 'padding-left:10px;'><a href = 'tel:$row[office_number]'>".$row['office_number']."</a></td>"; echo "<td style = 'padding-left:10px;'><a href = 'tel:$row[other_mob_no]'>".$row['other_mob_no']."</a></td>"; echo "<td style = 'padding-left:10px;'><a href = 'tel:$row[mob_no]'> <button class = 'btn btn-info'><span class='glyphicon glyphicon-phone'></span></button> </a></td>"; echo "</tr>"; endwhile; } } else { require('dbconnection.php'); $sql = "SELECT * FROM `tbl_new_contact` WHERE user_code = '$_SESSION[user_code]'"; $query = mysql_query($sql); while($row = mysql_fetch_array($query)): echo "<tr height = '25px'>"; echo "<td style = 'padding-left:10px;'>".$row['name']."</td>"; echo "<td style = 'padding-left:10px;'>".$row['address']."</td>"; echo "<td style = 'padding-left:10px;'>".$row['mob_no']."</td>"; echo "<td style = 'padding-left:10px;'>".$row['office_name']."</td>"; echo "<td style = 'padding-left:10px;'><a href = 'tel:$row[office_number]'>".$row['office_number']."</a></td>"; echo "<td style = 'padding-left:10px;'><a href = 'tel:$row[other_mob_no]'>".$row['other_mob_no']."</a></td>"; echo "<td style = 'padding-left:10px;'><a href = 'tel:$row[mob_no]'> <button class = 'btn btn-info'><span class='glyphicon glyphicon-phone'></span></button> </a></td>"; echo "</tr>"; endwhile; } ?> </table> </div> <div id = "frmNC_footer"></div> </div> </body> </html> ,则可以在第三个参数中定义其他Regex::Match选项。在这里使用regex

[System.Text.RegularExpressions.RegexOptions]::Singleline

如果您要使用Select-String cmdlet,则必须在$html = @' <li> test </li> </ul> '@ $regex = '(^.*<li>.*\</ul>)' [regex]::Match($html,$regex,[System.Text.RegularExpressions.RegexOptions]::Singleline).Groups[0].Value 中指定单行选项(?s)

regex

答案 1 :(得分:1)

使用多行单行正则表达式,-match

$string = @'
notmached
<li> test </li>
</ul>
notmatched
'@


$regex = @'
(?ms)(<li>.*</li>.*?
\s*</ul>)
'@

$string -match $regex > $null
$matches[1]

<li> test </li>
</ul>