qsy666888 发表于 2014-5-17 15:36:34

求教怎么才能查找所有括号里面的字符串(已解决)

本帖最后由 qsy666888 于 2014-5-22 18:33 编辑

txt文件里有:
(附属工程)
办公楼
商务部
写字楼
(住宅楼)
高利贷
商务楼
(商业部)
商场
菜市场

怎么才能找出所有带括号里面的字符,并一起显示在msgbox里

gto250 发表于 2014-5-17 15:52:30

_StringBetween 这个UDF很好用呀

#include<array.au3>


$str="(附属工程)"&@CRLF & _
   "办公楼"& @CRLF & _
       "商务部"& @CRLF & _
       "写字楼"& @CRLF & _
       "(住宅楼)"& @CRLF & _
       "高利贷"& @CRLF & _
       "商务楼"& @CRLF & _
       "(商业部)"& @CRLF & _
       "商场"& @CRLF & _
       "菜市场"
$re=_StringBetween($str,"(",")")
_ArrayDisplay($re)


Func _StringBetween($sString, $sStart, $sEnd, $iMode = $STR_ENDISSTART, $fCase = False)
        ; Set mode
    If $iMode <> $STR_ENDNOTSTART Then $iMode = $STR_ENDISSTART

    ; Set correct case sensitivity
    If $fCase = Default Then
      $fCase = False
    EndIf
    Local $sCase = "(?is)"
    If $fCase Then
      $sCase = "(?s)"
    EndIf

    ; If starting from beginning of string
    $sStart = $sStart ? "\Q" & $sStart & "\E" : "\A"

    ; If ending at end of string
    If $iMode = $STR_ENDISSTART Then
                ; Use lookahead
                $sEnd = $sEnd ? "(?=\Q" & $sEnd & "\E)" : "\z"
    Else
                ; Capture end string
                $sEnd = $sEnd ? "\Q" & $sEnd & "\E" : "\z"
    EndIf

    Local $aReturn = StringRegExp($sString, $sCase & $sStart & "(.*?)" & $sEnd, $STR_REGEXPARRAYGLOBALMATCH)
    If @error Then Return SetError(1, 0, 0)
    Return $aReturn
EndFunc   ;==>_StringBetween

qsy666888 发表于 2014-5-17 15:58:02

回复 2# gto250

谢谢大侠的回复,不过我问的是查找在txt文本里字符,文本里有可能比这还多,想把里面带括号里的名字全找出来。

gto250 发表于 2014-5-17 19:51:03

不一样吗?
你试一下就知道了

qsy666888 发表于 2014-5-17 20:00:58

回复 4# gto250


运行不起来,出现错误

qsy666888 发表于 2014-5-17 20:01:35

"L:\图书归类管理\树状\新建 AutoIt v3 脚本 (2).au3"(14,71) : warning: $STR_ENDISSTART: 使用前并未进行声明.
Func _StringBetween($sString, $sStart, $sEnd, $iMode = $STR_ENDISSTART,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"L:\图书归类管理\树状\新建 AutoIt v3 脚本 (2).au3"(16,35) : warning: $STR_ENDNOTSTART: 使用前并未进行声明.
    If $iMode <> $STR_ENDNOTSTART Then
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"L:\图书归类管理\树状\新建 AutoIt v3 脚本 (2).au3"(26,23) : error: 表达式错误 (非法字符)
    $sStart = $sStart ?
~~~~~~~~~~~~~~~~~~~~~~^
"L:\图书归类管理\树状\新建 AutoIt v3 脚本 (2).au3"(26,47) : error: 表达式错误 (非法字符)
    $sStart = $sStart ? "\Q" & $sStart & "\E" :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"L:\图书归类管理\树状\新建 AutoIt v3 脚本 (2).au3"(30,31) : error: 表达式错误 (非法字符)
                $sEnd = $sEnd ?

gto250 发表于 2014-5-17 21:21:22

我不知道你是怎么写的代码!!!
$re=_StringBetween(FileRead("2.txt"),"(",")")

2.txt中就是
(附属工程)
办公楼
商务部
写字楼
(住宅楼)
高利贷
商务楼
(商业部)
商场
菜市场


这些文字
我这里运行是没有任何问题的

#include<array.au3>



$re=_StringBetween(FileRead("2.txt"),"(",")")
_ArrayDisplay($re)


Func _StringBetween($sString, $sStart, $sEnd, $iMode = $STR_ENDISSTART, $fCase = False)
        ; Set mode
    If $iMode <> $STR_ENDNOTSTART Then $iMode = $STR_ENDISSTART

    ; Set correct case sensitivity
    If $fCase = Default Then
      $fCase = False
    EndIf
    Local $sCase = "(?is)"
    If $fCase Then
      $sCase = "(?s)"
    EndIf

    ; If starting from beginning of string
    $sStart = $sStart ? "\Q" & $sStart & "\E" : "\A"

    ; If ending at end of string
    If $iMode = $STR_ENDISSTART Then
                ; Use lookahead
                $sEnd = $sEnd ? "(?=\Q" & $sEnd & "\E)" : "\z"
    Else
                ; Capture end string
                $sEnd = $sEnd ? "\Q" & $sEnd & "\E" : "\z"
    EndIf

    Local $aReturn = StringRegExp($sString, $sCase & $sStart & "(.*?)" & $sEnd, $STR_REGEXPARRAYGLOBALMATCH)
    If @error Then Return SetError(1, 0, 0)
    Return $aReturn
EndFunc   ;==>_StringBetween

qsy666888 发表于 2014-5-17 21:32:59

不知道怎么回事,运行不起来,跟上面一样,出现代码错误

shqf 发表于 2014-5-17 22:06:16

Local $text = ""
Local $var = FileRead("1.txt")
Local $aArray = StringRegExp($var, '\((.+?)\)', 3)
For $i = 0 To UBound($aArray) - 1
        $text = $text & $aArray[$i]
Next
MsgBox("", "括号内的文本 ", $text)1.txt与脚本 文件放在一起。

qsy666888 发表于 2014-5-17 22:12:03

是放在一个文件夹里的

qsy666888 发表于 2014-5-17 22:12:21

回复 9# shqf


    是放在一个文件夹里的

qsy666888 发表于 2014-5-17 22:14:36

回复 11# qsy666888


谢谢大侠的帮助,非常感谢

qsy666888 发表于 2014-5-17 22:15:03

回复 9# shqf


   谢谢大侠的帮助,非常感谢

qsy666888 发表于 2014-5-17 22:19:33

回复 9# shqf


要是能换行显示就好了

qsy666888 发表于 2014-5-17 22:21:53

或者用分隔符 | 也行
页: [1] 2
查看完整版本: 求教怎么才能查找所有括号里面的字符串(已解决)