关于正则,我真的投降,快来帮帮我~`
本帖最后由 tttbbb999 于 2012-3-18 11:11 编辑<tr>
<td>a1</td>
<td>a1.exe</td>
<td>9</td>
<td>250</td>
<td>2</td>
</tr>
<tr>
<td>a2</td>
<td>a2.exe</td>
<td>8</td>
<td>250</td>
<td>2</td>
</tr>
;====================
$var = 2
$var = a1
$var = a1.exe
$var = 9
$var = 250
$var = 2
$var = a2
$var = a2.exe
$var = 8
$var = 250
$var = 2
要怎么写呀。头疼。
==============================
提取后字符串为: <td>a1</td><td>a1.exe</td><td>9</td><td>250</td><td>2</td>
: <td>a1</td><td>a1.exe</td><td>9</td><td>250</td><td>2</td>
2个字符串,用于再次提取。
;================================
谢谢你们大家了。。我换了个思路:
$result = StringRegExpReplace($soure, "\s*","") ;替换掉空格
$result2 = StringRegExp($result,"<tr>(.*?)</tr>",3) ;分组
_ArrayDisplay($result2)
后面可以再用正则检查一次,就可以写进2维数组了。 <td>(.*)</td> [^<>]+?(?=\<) [^]+?(?=\
kevinch 发表于 2012-3-18 10:20 http://www.autoitx.com/images/common/back.gif
能把空白忽略掉吗? #include <Array.au3>
$Str = _
'<tr>' & @CRLF & _
' <td>a1</td>' & @CRLF & _
' <td>a1.exe</td>' & @CRLF & _
' <td>9</td>' & @CRLF & _
' <td>250</td>' & @CRLF & _
' <td>2</td>' & @CRLF & _
' ' & @CRLF & _
' </tr> ' & @CRLF & _
'' & @CRLF & _
' <tr>' & @CRLF & _
' <td>a2</td>' & @CRLF & _
' <td>a2.exe</td>' & @CRLF & _
' <td>8</td>' & @CRLF & _
' <td>250</td>' & @CRLF & _
' <td>2</td>' & @CRLF & _
' ' & @CRLF & _
' </tr>' & @CRLF & _
''
$array = StringRegExp($Str, '<(?i)td>(.*?)</(?i)td>', 3)
_ArrayDisplay($array, "匹配结果")
#include <Array.au3>
Local $Str = _
'<tr>' & @CRLF & @CRLF & _
' <td>a1 </td>' & @CRLF & @CRLF & _
' <Td> a1.exe</tD>' & @CRLF & @CRLF & _
' <td>9 </td>' & @CRLF & @CRLF & _
' <td>250</td>' & @CRLF & @CRLF & _
' <td> 2</td>' & @CRLF & @CRLF & _
' ' & @CRLF & @CRLF & _
' </tr> ' & @CRLF & @CRLF & _
'' & @CRLF & @CRLF & _
' <tr>' & @CRLF & @CRLF & _
' <td> a2</td>' & @CRLF & @CRLF & _
' <td>a2.exe </td>' & @CRLF & @CRLF & _
' <td>8</td>' & @CRLF & @CRLF & _
' <td>250</td>' & @CRLF & @CRLF & _
' <td>2</td>' & @CRLF & @CRLF & _
' ' & @CRLF & @CRLF & _
' </tr>' & @CRLF & @CRLF
MsgBox(0, '原字符串', $Str)
Local $Test = StringRegExp($str, '(?i)<td>\s*([^<\s]+)', 3)
If Not @Error Then MsgBox(0, '匹配数量: ' & UBound($Test), '其中元素为: ' & $Test)
_ArrayDisplay($Test, UBound($Test)) 感谢了,实际数量会更多,这样的话数据不好分类。得用二维数组才行。
已经想到办法了,见顶楼,感谢大家。 原来是这个意思,是我没看明白楼主的意思.
lixiaolong 发表于 2012-3-18 11:04 http://www.autoitx.com/images/common/back.gif
为什么每行用两个@CRLF呢
& @CRLF & @CRLF & _ 回复 9# zldfsz
直接复制一楼的字符串后用Au3.REHelper转换就这样了... 点击“复制代码”才不会增加多余的空行,呵呵 回复zldfsz
直接复制一楼的字符串后用Au3.REHelper转换就这样了...
lixiaolong 发表于 2012-3-18 11:59 http://www.autoitx.com/images/common/back.gif
点击“复制代码”才不会增加多余的空行,呵呵
afan 发表于 2012-3-18 12:16 http://www.autoitx.com/images/common/back.gif
怪不得呢,我说我转的就没有。。。不过我之前还真没注意Au3.REHelper可以直接转换生成,我之前还用的 txt2au3转为字符串呢,呵呵见笑了 本帖最后由 shqf 于 2012-3-18 12:47 编辑
给出我的方法,如下:(1.txt为字符串所在的文件,如实际<tr>、<td>块多的话,加大数组定义)
#include <Array.au3>
Local $Array
$File = FileOpen(@ScriptDir & "\1.txt", 0)
$Var = FileRead($File)
$tr = StringRegExp($Var, "<tr>([\s\S]+?)</tr>", 3)
$Array = UBound($tr)
For $i = 0 To UBound($tr)-1
$td = StringRegExp($tr[$i], "<td>([\s\S]+?)</td>", 3)
For $n = 0 To UBound($td) - 1
$Array[$i+1][$n] = $td[$n]
Next
Next
FileClose($file)
_ArrayDisplay($Array) 回复 12# zldfsz
唉,很多功能其实都是因为没看帖子说明,所以,我现在已不作详细介绍了 回复 14# afan
因为平时都主要用于测试表达式的结果,所以就没。。。
页:
[1]
2