如何在不删除分隔符的情况下拆分字符串?

时间:2018-01-23 15:38:35

标签: string split autoit

我的AutoIt脚本按句子解析文本。因为它们很可能以句号,问号或感叹号结束,所以我用它来逐句分割文字:

$LineArray = StringSplit($displayed_file, "!?.", 2)

问题;它会删除分隔符(句子末尾的句号,问号和感叹号)。例如,字符串One. Two. Three.分为OneTwoThree

如何在保留结束这些句子的句号,问号和感叹号的同时分成句子?

2 个答案:

答案 0 :(得分:0)

试试这个:

#include<Array.au3>
Global $str = "One. Two. Three. This is a test! Does it work? Yes, man! "
$re = StringRegExp($str, '(.*?[.!?])', 3)
_ArrayDisplay($re)

此模式在句子开头没有空格

#include<Array.au3>
Global $str = "One. Two. Three.This is a test! Does it work? Yes, man! "
$re = StringRegExp($str, '(\S.*?[.!?])', 3)
_ArrayDisplay($re)

答案 1 :(得分:0)

使用StringSplit()过程中会使用分隔符(结果会丢失分隔符)。使用StringRegExp()

#include <array.au3>
$string="This is a text. It has several sentences. Really? Of Course!"
$a = stringregexp($string,"(?U)(.*[.?!])",3)
_ArrayDisplay($a)

要删除前导空格,请将模式更改为"(?U)[ ]*?(.*[.?!])"。或"(?U) *?(.*[.?!] )"分割为[.!?]<space>(在最后一句中添加空格):

#include <array.au3>
$string = "Do you know Pi?   Yes!   What's it?    It's 3.14159!   That's correct."
$a = StringRegExp($string & " ", "(?U)[ ]*?(.*[.?!] )", 3)
_ArrayDisplay($a)

在句子中保留@CRLF\r\n):

#include <array.au3>
$string = "Do you " & @CRLF & "know Pi?   Yes!  What's it?    It's" & @CRLF & "3.14159!   That's correct."
$a = StringRegExp($string & "  ", "(?s)(?U)[ ]*?(.*[.?!][ \R] )", 3)
_ArrayDisplay($a,"Sentences")   ;_ArrayDisplay doesn't show @CRLF

For $i In $a
    ;MsgBox(0,"",$i)
    ConsoleWrite(StringStripWS($i, 3) & @CRLF & "---------" & @CRLF)
Next

当行尾与句子结尾相同时,@CRLF不会保留...line end!" & @CRLF & "Next line...

相关问题