[已解决]正则能不能匹配字符串的后半截?
本帖最后由 雨林GG 于 2011-5-19 20:40 编辑请大侠帮忙:如下字符串,每一条中都含有由空格分隔的五项内容,在后三项相同的情况下,只保留第一条。
不知用正则,能不能得到加下划线的字符串?
$str = "2011-05-17 02:56:23 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/" & @CRLF & _
"2011-05-17 02:56:25 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/" & @CRLF & _
"2011-05-17 02:56:28 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/" & @CRLF & _
"2011-05-17 02:56:28 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/xxpt/ShowArticle.asp?ArticleID=25512" & @CRLF & _
"2011-05-17 02:57:06 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/" & @CRLF & _
"2011-05-17 02:57:06 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/xxjj/ShowClass.asp?ClassID=262" & @CRLF & _
"2011-05-17 02:57:07 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/xxjj/ShowClass.asp?ClassID=262" & @CRLF & _
"2011-05-17 02:57:29 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/xxjj/ShowClass.asp?ClassID=262&page=2" & @CRLF & _
"2011-05-17 02:58:23 192.168.0.1 PEDCKDDANGAGMGACEGBCBCFC htp://192.168.0.254/" & @CRLF & _
"2011-05-17 02:58:24 192.168.0.1 PEDCKDDANGAGMGACEGBCBCFC htp://192.168.0.254/"
;$Test = StringRegExpReplace($str, '(.*?\n)\1+', '$1') ;删除相邻重复行
MsgBox(0, '替换结果', $Test) Local $Str = _
'2011-05-17 02:56:23 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:25 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:28 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:28 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/xxpt/ShowArticle.asp?ArticleID=25512' & @CRLF & _
'2011-05-17 02:57:06 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:57:06 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/xxjj/ShowClass.asp?ClassID=262' & @CRLF & _
'2011-05-17 02:57:07 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/xxjj/ShowClass.asp?ClassID=262' & @CRLF & _
'2011-05-17 02:57:29 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/xxjj/ShowClass.asp?ClassID=262&page=2' & @CRLF & _
'2011-05-17 02:58:23 192.168.0.1 PEDCKDDANGAGMGACEGBCBCFC htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:58:24 192.168.0.1 PEDCKDDANGAGMGACEGBCBCFC htp://192.168.0.254/' & @CRLF
Local $Test = StringRegExpReplace($str, '(.*?)({12,}.*?\r\n)(?=.*?\2)', '')
MsgBox(0, '替换结果', $Test)
本帖最后由 雨林GG 于 2011-5-18 20:57 编辑
回复 2# 3mile
谢谢您的帮助 ! 向您学习!!!
就是相同项留下了最后一条,而不是第一条~!
后面那个?=什么意思 ? 你这代码就不能标记出来吗,看得乱死.搞了半天还弄不明白你要的是啥 ;做正则替换题 by easefull
Local $sText = _
'2011-05-17 02:56:23 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:25 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:28 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:28 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:28 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/xxpt/ShowArticle.asp?ArticleID=25512' & @CRLF & _
'2011-05-17 02:57:06 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:57:06 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/xxjj/ShowClass.asp?ClassID=262' & @CRLF & _
'2011-05-17 02:57:07 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/xxjj/ShowClass.asp?ClassID=262' & @CRLF & _
'2011-05-17 02:57:29 192.168.0.2 KADCKDDAKPNMGJMIIDCKFEJF htp://192.168.0.254/xxjj/ShowClass.asp?ClassID=262&page=2' & @CRLF & _
'2011-05-17 02:58:23 192.168.0.1 PEDCKDDANGAGMGACEGBCBCFC htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:58:24 192.168.0.1 PEDCKDDANGAGMGACEGBCBCFC htp://192.168.0.254/'
$sText &= @CRLF
Do
$sText = StringRegExpReplace($sText, '(\S+\s+\S+\s+)(.*?\r\n)(\S+\s+\S+\s+\2)', '\1\2')
Until Not StringRegExp($sText, '(\S+\s+\S+\s+)(.*?\r\n)(\S+\s+\S+\s+\2)', 0)
$sText = StringRegExpReplace($sText, '\r\n$', '')
ClipPut($sText)
MsgBox(0, '替换结果', $sText) 本帖最后由 easefull 于 2011-5-18 22:14 编辑
我这代码必须是加do..until的.因为碰到同一电脑多次(大于2次)刷新同一页面的记录时,如
Local $sText = _
'2011-05-17 02:56:23 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:25 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:28 192.168.0.2 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/' & @CRLF & _
'2011-05-17 02:56:28 192.168.0.1 MDDCKDDAHLBAJIGHONEGJJBH htp://192.168.0.254/'
会漏处理重复连续记录的第3个记录.
至于大文件的do...until,应该不会造成cpu占用率过高.
因为第二次处理的时候只是上面漏处理的那一行.
但我很是拜服3mile的代码——那怎么为什么不需要do...until处理也能达到同样的效果呢?
页:
[1]