Qokelate 发表于 2012-8-24 05:08:14

已解决 求正则 如何用正则取出一段文本?

本帖最后由 Qokelate 于 2012-8-24 17:40 编辑



=========================================================
程序名称:AutoIt
汉化作者:
ALL:thesnoW
ADF:Sxd,rikthhpgf2005
EXP:kodin,Alan
中文论坛: http://www.AutoItX.com
start==>
绿色安装方法:解压缩到 任意目录.
绿色卸载方法:不爽删除就是.
正常安装方法:直接运行自解压程序.
正常卸载汉化:使用AU3工具箱>程序相关设置>卸载这个程序.
命令行安装: "au3tool.exe /s"
命令行卸载: "au3tool.exe /u"
AutoIt工具箱提供了安装卸载功能.
<==End
绿色安装方法:解压缩到 任意目录.
绿色卸载方法:不爽删除就是.
正常安装方法:直接运行自解压程序.
正常卸载汉化:使用AU3工具箱>程序相关设置>卸载这个程序.
命令行安装: "au3tool.exe /s"
命令行卸载: "au3tool.exe /u"
AutoIt工具箱提供了安装卸载功能.
======================================================
绿色安装方法:解压缩到 任意目录.
绿色卸载方法:不爽删除就是.
正常安装方法:直接运行自解压程序.
正常卸载汉化:使用AU3工具箱>程序相关设置>卸载这个程序.
命令行安装: "au3tool.exe /s"
命令行卸载: "au3tool.exe /u"
AutoIt工具箱提供了安装卸载功能.
======================================================

要求 (取出的行均不包含 start==> 或 <==End 本身 )

1. 如果不存在 start==> 行则不进行操作
2. 如果文本中存在单独的行start==> 和 <==End 则取出它们之间的行
3. 如果文本中存在单独的行 start==> 但不存在 <==End 则取出 start==> 至文本最后内容

xiehuahere 发表于 2012-8-24 09:51:06

本帖最后由 xiehuahere 于 2012-8-24 10:15 编辑

回复 1# Qokelate

将你给出的文本写入file.txt测试。

Local Const $oFile = ".\file.txt"
Local $aRecords, $str, $result

If Not FileExists($oFile) Then Exit MsgBox(48 + 262144, "Warning", 'File "' & $oFile & '" does not exit!')
$str = FileRead($oFile)
If Not StringInStr($str, "start==>") Then Exit
If StringInStr($str, "<==End") Then
        $result = StringRegExpReplace($str, '(?is).*?start==>(.*?)<==End.*', '$1', 1)
Else
        $result = StringRegExpReplace($str, '(?is).*?start==>(.*)\z', '$1', 1)
EndIf
MsgBox(0, "", $result)

xiehuahere 发表于 2012-8-24 10:28:10

直接用正则自适应<==End存在和不存在的情况,也能取出符合要求的内容。

Local Const $oFile = ".\file.txt"
Local $array, $str

If Not FileExists($oFile) Then Exit MsgBox(48 + 262144, "Warning", 'File "' & $oFile & '" does not exit!')
$str = FileRead($oFile)
If Not StringInStr($str, "start==>") Then Exit
$array = StringRegExp($str, '(?is)start==>(.*?)((<==End.*)|\z)', 3)
If @error <> 0 Then Exit MsgBox(48 + 262144, "RegExp error", @error)
MsgBox(0, "", $array)

afan 发表于 2012-8-24 10:59:14

(?si)start==>\h*\v*(.*?)\v*(?=<==End|$)

xiehuahere 发表于 2012-8-24 17:07:13

回复 4# afan

看到正则强人出来了,顺便讨教两点:

1) 敢问 \h \v 制表符在这里起啥作用? 点号 . 不是能匹配任意字符的吗?

2) .*?(exp)是否等同于 .*?(?=exp) ?
   (?=exp) 匹配表达式exp前面的位置,.*? 本来就是最小匹配,肯定在exp之前。
    我这样理解对吗?

Qokelate 发表于 2012-8-24 17:40:23

谢谢关注问题解决   感谢各位~~~:face (33):

afan 发表于 2012-8-24 19:56:12

回复afan

1) 敢问 \h \v 制表符在这里起啥作用? 点号 . 不 ...
xiehuahere 发表于 2012-8-24 17:07 http://www.autoitx.com/images/common/back.gif


    \h* 允许 “start==>” 后面有空格,\v* 则不会捕捉到首个空行。

.*?(exp)不等同于 .*?(?=exp)
前面的 exp 为有效组,后面的 exp 不为有效组

xiehuahere 发表于 2012-8-24 21:06:41

回复 7# afan


    嗯,知道区别了,多谢!
页: [1]
查看完整版本: 已解决 求正则 如何用正则取出一段文本?