powershell中str.IndexOf('')这样搜索为什么不好使? 我想过滤标题

jerryleeee 2016-02-15 06:52:08

在写个脚本过滤所有html文件的<title>标题</title> 之间的中文标题,

$fileList = Get-ChildItem  'D:\temp' -filter *.shtml -recurse
foreach($file in $fileList)
{
$content = [string](get-content $file.pspath)
foreach($line in $content)
{
$i = $line.IndexOf("<title>")
if ($i -gt 0 )
{
$j = $line.IndexOf('</title>')
$i = $i + 8
$biaotiStr = $line.Substring($i,$j-$i) #这里出错,因为$j 取不到
echo $biaotiStr
}
}
}
...全文
273 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jerryleeee 2016-02-15
  • 打赏
  • 举报
回复
果然是文件编码问题 get-content $file.pspath -Encoding UTF8 这样就正常了!
Poopaye 2016-02-15
  • 打赏
  • 举报
回复
用记事本保存的文本,都是能搜到的
jerryleeee 2016-02-15
  • 打赏
  • 举报
回复
被搜索的html文件是utf8编码,难道跟标题的中文识别乱码有关系?
引用 8 楼 jerryleeee 的回复:
这是被搜索的html文件 [quote=引用 6 楼 shingoscar 的回复:] [quote=引用 4 楼 jerryleeee 的回复:] $j = $line.IndexOf('/title>') 这样就能搜索到,好奇怪 ,为什么
那应该是 / 前面有别的字符[/quote][/quote]
jerryleeee 2016-02-15
  • 打赏
  • 举报
回复
这是被搜索的html文件


引用 6 楼 shingoscar 的回复:
[quote=引用 4 楼 jerryleeee 的回复:]
$j = $line.IndexOf('/title>')

这样就能搜索到,好奇怪 ,为什么

那应该是 / 前面有别的字符[/quote]
jerryleeee 2016-02-15
  • 打赏
  • 举报
回复
说的是!改了改,但还是不行。。。 百思不得骑姐。。
$fileList = Get-ChildItem  'D:\temp' -filter *.html -recurse
foreach($file in $fileList)
{
    $content = [string](get-content $file.pspath)

               $i = $content .IndexOf("<title>")
                if ($i -gt 0 )
        {
                    $j = $content .IndexOf('</title>')
                     $i = $i + 8
                     #$biaotiStr = $content .Substring($i,$j-$i)   #这里出错,因为$j 取不到
  
          }
           
}
引用 5 楼 shingoscar 的回复:
[quote=引用 4 楼 jerryleeee 的回复:] [quote=引用 2 楼 shingoscar 的回复:] 说明不在一行里啊
$content = [string](get-content $file.pspath) 在这里 [string] 已经把内容全部作为字符串去过来了,如果没有 [string] ,是一行行的数组 $j = $line.IndexOf('/title>') 这样就能搜索到,好奇怪 ,为什么[/quote] $content是全部内容,那后面的foreach是什么意思????[/quote]
Poopaye 2016-02-15
  • 打赏
  • 举报
回复
引用 4 楼 jerryleeee 的回复:
$j = $line.IndexOf('/title>') 这样就能搜索到,好奇怪 ,为什么
那应该是 / 前面有别的字符
Poopaye 2016-02-15
  • 打赏
  • 举报
回复
引用 4 楼 jerryleeee 的回复:
[quote=引用 2 楼 shingoscar 的回复:] 说明不在一行里啊
$content = [string](get-content $file.pspath) 在这里 [string] 已经把内容全部作为字符串去过来了,如果没有 [string] ,是一行行的数组 $j = $line.IndexOf('/title>') 这样就能搜索到,好奇怪 ,为什么[/quote] $content是全部内容,那后面的foreach是什么意思????
jerryleeee 2016-02-15
  • 打赏
  • 举报
回复
引用 2 楼 shingoscar 的回复:
说明不在一行里啊
$content = [string](get-content $file.pspath) 在这里 [string] 已经把内容全部作为字符串去过来了,如果没有 [string] ,是一行行的数组 $j = $line.IndexOf('/title>') 这样就能搜索到,好奇怪 ,为什么
jerryleeee 2016-02-15
  • 打赏
  • 举报
回复
引用 1 楼 crystal_lz 的回复:
Console.WriteLine(Regex.Match(strHtml,@"<title[^>]*>(.*?)</title>",RegexOptions.IgroneCase))
放到上面脚本运行报错。。。。
$fileList = Get-ChildItem  'D:\temp' -filter *.shtml -recurse
foreach($file in $fileList)
{
    $content = [string](get-content $file.pspath)
        foreach($line in $content)
    {
               $i = $line.IndexOf("<title>")
                if ($i -gt 0 )
        {
                    $j = $line.IndexOf('</title>')
                     $i = $i + 8
                     #$biaotiStr = $line.Substring($i,$j-$i)   #这里出错,因为$j 取不到
  Console.WriteLine(Regex.Match($line,@"<title[^>]*>(.*?)</title>",RegexOptions.IgroneCase))
                    
                 }
            }
}
Poopaye 2016-02-15
  • 打赏
  • 举报
回复
说明不在一行里啊
crystal_lz 2016-02-15
  • 打赏
  • 举报
回复
Console.WriteLine(Regex.Match(strHtml,@"<title[^>]*>(.*?)</title>",RegexOptions.IgroneCase))

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧