标题: [问题求助] 【已解决】请教文本处理的方法。最好能用RegExp正则表达式处理 [打印本页]
作者: loveforjg 时间: 2020-11-25 09:49 标题: 【已解决】请教文本处理的方法。最好能用RegExp正则表达式处理
本帖最后由 loveforjg 于 2020-12-8 13:56 编辑
万能的BBS,请教各位高手!
需要将下面带 “[ ]”文本内容找出来,每个方括号的内容输出为一行,并能排序输出。
输入文本:
[
AAA
BBBB
CCCCCC
]
[
D
EE
FFFFF
]
输出格式:
[ BBBB AAA CCCCCC ]
[ EE D FFFFF]
作者: ivor 时间: 2020-11-25 11:19
[ BBBB AAA CCCCCC ]
[ EE D FFFFF ]
请按任意键继续. . .
- #&cls&@powershell "Invoke-Expression ([Io.File]::ReadAllText('%~0',[Text.Encoding]::UTF8))" &pause&exit
- $str = "
- [
- AAA
- BBBB
- CCCCCC
- ]
-
- [
- D
- EE
- FFFFF
- ]"
- [regex]::Matches($str,"\[[\s\S]+?\]") | %{$str_arr=($_.Value -split "\r\n");$str_arr[1],$str_arr[2]=$str_arr[2],$str_arr[1];$str_arr -join " "}
复制代码
作者: flashercs 时间: 2020-11-25 13:35
- Option Explicit
- ' On Error Resume Next
- If WScript.Arguments.Count = 0 Then WScript.Quit 1
- Const conRead = 1
- Const conWrite = 2
- Const conAppend = 8
- Dim fso,sr,str,Match
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set sr = fso.OpenTextFile(WScript.Arguments(0),conRead,False,vbUseDefault)
- Do Until sr.AtEndOfStream
- str = sr.ReadAll
- Loop
- sr.Close
- ' WScript.Echo str
- With New RegExp
- .Global = True
- .Ignorecase = True
- .Multiline = False
- .Pattern = "(\[)[^\r\n]*[\r\n]+([^\r\n]*)[\r\n]+([^\r\n]*)[\r\n]+([^\r\n]*)[\r\n]+.*(\])"
- WScript.Echo .Replace(str,"$1 $3 $2 $4 $5")
- End With
- Set sr = Nothing
- Set fso = Nothing
复制代码
用法:this.vbs a.txt
作者: loveforjg 时间: 2020-11-25 14:53
回复 3# flashercs
谢谢您的代码,但好像没成功。
能不用拖曳模式吗,输入a.txt, 输出b.txt。目前就是正则表达式“Pattern”这里很疑惑
作者: loveforjg 时间: 2020-11-25 14:54
回复 2# ivor
看不懂您这个代码,是VBS吗,谢谢您的回复
作者: flashercs 时间: 2020-11-25 15:55
回复 4# loveforjg - Option Explicit
- ' On Error Resume Next
- Const conRead = 1
- Const conWrite = 2
- Const conAppend = 8
- Dim fso,sr,sw,str,src,dst
- src = "a.txt"
- dst = "b.txt"
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set sr = fso.OpenTextFile(src,conRead,False,vbUseDefault)
- Do Until sr.AtEndOfStream
- str = sr.ReadAll
- Loop
- sr.Close
- Set sw = fso.OpenTextFile(dst,conWrite,True,vbUseDefault)
- With New RegExp
- .Global = True
- .Ignorecase = True
- .Multiline = False
- .Pattern = "(\[)[^\r\n]*[\r\n]+([^\r\n]*)[\r\n]+([^\r\n]*)[\r\n]+([^\r\n]*)[\r\n]+.*(\])"
- sw.Write .Replace(str,"$1 $3 $2 $4 $5")
- End With
- sw.Close
- Set sr = Nothing
- Set sw = Nothing
- Set fso = Nothing
复制代码
作者: loveforjg 时间: 2020-11-26 10:48
回复 6# flashercs
老大威武!再请教下,如果文本出现多行,只需要替换前两行数据的位置,后面的括号内的数据不论多少行都直接输出,公式咋改呢?
[
A
B
C1
C2
C3
...
]
输出:[B A C1 C2 C3 .......]
作者: flashercs 时间: 2020-11-26 13:13
回复 7# loveforjg - Option Explicit
- ' On Error Resume Next
- Const conRead = 1
- Const conWrite = 2
- Const conAppend = 8
- Dim fso,sr,sw,str,src,dst,Match,arrLines,strtmp
- src = "a.txt"
- dst = "b.txt"
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set sr = fso.OpenTextFile(src,conRead,False,vbUseDefault)
- Do Until sr.AtEndOfStream
- str = sr.ReadAll
- Loop
- sr.Close
- Set sw = fso.OpenTextFile(dst,conWrite,True,vbUseDefault)
- With New RegExp
- .Global = True
- .Ignorecase = True
- .Multiline = False
- .Pattern = "\[[^\[\]]*\]"
- For Each Match In .Execute(str)
- arrLines = Split(Match,vbNewLine,-1,vbBinaryCompare)
- If UBound(arrLines) > 2 Then ' 含至少2个元素才swap前2个
- strtmp = arrLines(1)
- arrLines(1) = arrLines(2)
- arrLines(2) = strtmp
- End If
- sw.WriteLine Join(arrLines," ")
- Next
- End With
- sw.Close
- Set sr = Nothing
- Set sw = Nothing
- Set fso = Nothing
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |