2015-06-22 3 views
0

PowerShell Я был бы признателен получить какое-то решение для моей проблемы, связанной с кодом синтаксического анализа/HTML файла регулярного выражения:Multiline регулярного выражение

D: \ acc.html

<!-- WebSite-Watcher Demo Report --> 



<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>WebSite-Watcher Report</title> 
<style type="text/css"> 
<!-- 
a:link, a:active { 
    color: #4040A0; 
    text-decoration: underline; 
} 
a:visited { 
    color: #8080A0; 
    text-decoration: underline; 
} 
a:hover { 
    background: #FFF000; 
    color: #FF0000; 
    text-decoration: underline; 
} 
body, td { 
    font-size: 11px; 
    line-height: 15px; 
    font-family: Verdana, Arial; 
} 
li { 
    list-style: square; 
    font-size: 11px; 
    line-height: 15px; 
    margin-top: 10px; 
} 
--> 
</style> 
</head> 

<body> 
<center> 

<table cellpadding="2" cellspacing="2" border="0" width="80%"> 
<tr> 
<td> 
<font color="#336699" style="font-size: 18px;"><b>Highlighted changes</b></font><br> 
<div style="border-top: 1px dashed dadada; margin-top: 5px;"></div> 
<br> 

<font color="#f00000">This report displays the first 200 characters of highlighted changes,<br> 
the length can be changed individually with the <b>wsw_url_highlighted_changes(200)</b> variable.</font><br> 
<br> 




<table cellpadding="5" cellspacing="0" border="0" width="100%"> 
<tr> 
<td style="border-bottom-color: #d0d0d0; border-bottom-style: solid; border-bottom-width: 1px; background-color: #eaeaea;"><!-- F1E896 --> 
<font style="font-size: 13px;"><b>Lorem ipsum</b></font><br><font color="#808080"> | <a href="http://www.hjccx.com/" target="_top">Web page</a> | <a href="file://x:/wswdb/wswdatabase_wsw/0004/2015052915594644815599.htm_chg.htm#wswchange1" target="_top">Local page</a></font> 
</td> 
</tr> 
<tr> 
<td style="border-bottom-color: #f0f0f0; border-bottom-style: solid; border-bottom-width: 1px; background-color: #f8f8f8;"><!-- F5F2C7 --> 
<blockquote> 
<br> 
</blockquote> 
</td> 
</tr> 
</table><br> 
<br> 


<table cellpadding="5" cellspacing="0" border="0" width="100%"> 
<tr> 
<td style="border-bottom-color: #d0d0d0; border-bottom-style: solid; border-bottom-width: 1px; background-color: #eaeaea;"><!-- F1E896 --> 
<font style="font-size: 13px;"><b>Lorem ipsum</b></font><br><font color="#808080">18-06-2015 | <a href="http://www.no target="_top">Web page</a> | <a href="file://x:/wswdb/wswdatabase_wsw/0004/2015052915594536915585.htm_chg.htm#wswchange1" target="_top">Local page</a></font> 
</td> 
</tr> 
<tr> 
<td style="border-bottom-color: #f0f0f0; border-bottom-style: solid; border-bottom-width: 1px; background-color: #f8f8f8;"><!-- F5F2C7 --> 
<blockquote> 
Lorem ipsum BBBBBBBBBBBB<br> 
</blockquote> 
</td> 
</tr> 
</table><br> 
<br> 

<table cellpadding="5" cellspacing="0" border="0" width="100%"> 
<tr> 
<td style="border-bottom-color: #d0d0d0; border-bottom-style: solid; border-bottom-width: 1px; background-color: #eaeaea;"><!-- F1E896 --> 
<font style="font-size: 13px;"><b>Lorem ipsum</b></font><br><font color="#808080">18-06-2015 | <a href="http://www.no target="_top">Web page</a> | <a href="file://x:/wswdb/wswdatabase_wsw/0004/2015052915594536915585.htm_chg.htm#wswchange1" target="_top">Local page</a></font> 
</td> 
</tr> 
<tr> 
<td style="border-bottom-color: #f0f0f0; border-bottom-style: solid; border-bottom-width: 1px; background-color: #f8f8f8;"><!-- F5F2C7 --> 
<blockquote> 
Lorem ipsum BBBBBBBBBBBB<br>AAAAAAAAAAAAAAAaa AA<br> 
</blockquote> 
</td> 
</tr> 
</table><br> 
<br> 


<br> 
<br> 

<div style="border-top: 1px dashed dadada;"></div> 
<font color="#808080"><i>Report date: 18-06-2015</i></font><br> 
</td> 
</tr> 
</table><br> 
</center> 
</body> 
</html> 

мне нужно «чистые» это не файл из пустых записей, как первый (нет текста только несколько пустых пространств или обычно просто

Я знаю, что в PowerShell есть решение для нескольких регулярных выражений, и это, вероятно, выглядеть следующим образом:.

d: \ pattern.txt

(?=<table cellpadding="5").*(?=<blockquote>).{0,6}(?=<\/blockquote>) 

сценарий: (спасибо Jisaak)

$content = (Get-Content 'd:\acc.txt' -raw) 
$pattern = (Get-Content 'd:\pattern.txt' -raw) 

[regex]::Replace($content, $pattern, '',` 
    [System.Text.RegularExpressions.RegexOptions]::Multiline ` 
    -bor [System.Text.RegularExpressions.RegexOptions]::Singleline) 

Я имею в виду из (0-6 любых символов))

Это регулярное выражение не У меня проблемы с правильной написанием этого расширенного регулярного выражения. Спасибо за любую помощь

+0

Это не ясно, что вы пытаетесь " clean ", может предоставить минимальный пример с образцом ввода и вывода? – ShellFish

+0

@ShellFish Извините, только что исправил его. – jawjaw

+0

Вы все еще должны быть более описательными относительно условия удаления таблицы, я не могу понять, почему одна таблица удалена, а другая - нет. – ShellFish

ответ

-1

Это должно работать:

(?<=<table cellpadding="5" cellspacing="0" border="0" width="100%">).* 
(?=<blockquote>)|(?<=<blockquote>).{0,6}(?=<\/blockquote>) 
+0

он удалил все, осталось только один кусок. я имею в виду один хороший левый и, если я изменяю 0,6 до 0,2, почти все удаляется. – jawjaw

1

ли эта проблема будет легче, если вы не имеете дело с несколькими линиями?

Мой опыт работы с регулярным выражением является ограниченным и HTML является несуществующей, но ниже обходной путь может превратить ваши блоки в отдельные строки (и обратно к блокам снова)

$file = (Get-Content ".\acc.html" -raw) 

# Replace new line CR LF with a string (e.g. NEWLINE) 
$file2 = ([regex]::Replace($file, ">`r`n", ">NEWLINE", "Singleline")) 
$file2 | out-file ".\acc_edited.html" 

# Single line regex replacement here to get rid of empty table. 
# String NEWLINE can be used to indicate a new line. 

# Replace the string with new line characters CR LF after regex replacement. 
[regex]::Replace($file2, ">NEWLINE", ">`r`n", "Singleline") | Out-File ".\acc_original.html"