找回密码
 加入
搜索
查看: 9262|回复: 16

[AU3基础] 关于字符串的前后空格去掉的正则表达式

 火.. [复制链接]
发表于 2015-5-16 21:22:46 | 显示全部楼层 |阅读模式
如何去掉字符串前后空格
发表于 2015-5-16 23:09:49 | 显示全部楼层
去空格不需表达式
Local $hhhh= '关于 字   符串的     前后  空 格去      掉的 正则  表达       式 '
$result = StringRegExpReplace($hhhh , ' ' ,'')
MsgBox(8096 , '' ,$result)
发表于 2015-5-17 00:17:17 | 显示全部楼层
本帖最后由 netegg 于 2015-5-17 00:21 编辑

回复 2# Huiseyu
不对吧,没有\A, \z怎么匹配开始和结束
发表于 2015-5-17 10:59:58 | 显示全部楼层
Local $a = " Hello world "
$a = StringTrimRight($a,1)        
$a = StringTrimLeft($a,1)        
MsgBox(0,0,$a)
发表于 2015-5-17 12:08:38 | 显示全部楼层
回复  Huiseyu
不对吧,没有\A, \z怎么匹配开始和结束
netegg 发表于 2015-5-17 00:17



    那个\A ,\z 匹配开始结束的好像在String.au3 里面,这个是正则替换。
发表于 2015-5-17 12:10:35 | 显示全部楼层
Func _StringBetween($sString, $sStart, $sEnd, $iMode = $STR_ENDISSTART, $bCase = False)
        ; Set mode
        If $iMode <> $STR_ENDNOTSTART Then $iMode = $STR_ENDISSTART

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

        ; 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
发表于 2015-5-17 12:14:27 | 显示全部楼层
本帖最后由 netegg 于 2015-5-17 12:18 编辑

不是udf,在自带函数里,确实是正则没错,你要的不就是开始和结尾的空格吗
发表于 2015-5-17 12:22:12 | 显示全部楼层
如果是所有的空格,那就stringreplace吧
发表于 2015-5-18 09:33:37 | 显示全部楼层
StringStripWS ( "字符串", 1 + 2)
发表于 2015-5-18 13:21:10 | 显示全部楼层
regexp:^\s+|\s+$
发表于 2015-5-18 16:20:21 | 显示全部楼层
本帖最后由 xlj310 于 2015-5-18 16:22 编辑

不知道楼主去前后空格为何要用正则表达式。如果实在要用正则来实现,完全是……就不多说了。
StringStripWS只去除英文空格,如果是中文空格也要去掉,那可以自己用字符串处理函数来处理。

Dim $str = "   this is  a string .     "

$start = 1
While 1
        If StringMid($str, $start, 1) <> " " And StringMid($str, $start, 1) <> " " Then ExitLoop
        $start+= 1
WEnd
$end = StringLen($str)
While 1
        If StringMid($str, $end, 1) <> " " And StringMid($str, $end, 1) <> " " Then ExitLoop
        $end-= 1
WEnd

$str=StringMid($str,$start,$end-$start+1)

MsgBox(0, "去前后空格的字符串", $str)
发表于 2015-5-19 14:53:16 | 显示全部楼层
StringRegExpReplace("字符串","\s","")
发表于 2015-5-19 15:21:06 | 显示全部楼层
回复 12# ap112


    你把所有的英文空格全去掉了(中文空格没有去掉) 呵呵 
发表于 2015-5-19 15:31:07 | 显示全部楼层
回复 13# xlj310


    我似乎对你的意图了解的不是很明白,你是想要???????????
发表于 2015-5-22 06:06:26 | 显示全部楼层
$returnval=StringRegExpReplace($mystring ,"(^\s*)|(\s*$)","")
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-28 00:41 , Processed in 0.081856 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表