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



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

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

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

如何在保留句点、问号和感叹号的同时拆分为句子?

使用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(rn(:

#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...

试试这个:

#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)

相关内容

  • 没有找到相关文章

最新更新