Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Powershell-从字符串中提取句子的最佳方法是什么_String_Powershell_Text Extraction - Fatal编程技术网

String Powershell-从字符串中提取句子的最佳方法是什么

String Powershell-从字符串中提取句子的最佳方法是什么,string,powershell,text-extraction,String,Powershell,Text Extraction,我有很多这样的结构行的文本 Sentence a. Sentence b part 1 `r`n sentence b part 2. Sentence c.`r`n Sentence d. Sentence e. Sentence f. `r`n .... 我想把这些句子和部分提取到一个字符串数组中,每个部分或一个句子。 到现在为止,我发现了这些东西 第一条路 $mySentences = $lineFromTheText -split "(?<=\.)" 第三个

我有很多这样的结构行的文本

Sentence a. Sentence b part 1 `r`n
sentence b part 2. Sentence c.`r`n
Sentence d. Sentence e. Sentence f. `r`n
....
我想把这些句子和部分提取到一个字符串数组中,每个部分或一个句子。 到现在为止,我发现了这些东西

第一条路

$mySentences = $lineFromTheText -split "(?<=\.)"
第三个代码

$mySentences = ($lineFromTheText | Select-String -Pattern "([^.?!]+[.?!])?([^.?!]*$)?" -AllMatches).Matches  | % {$_.Groups[1,2].Value} | % { If (-not ($_ -eq "")) {$_}}
似乎所有这些代码都为我做了与我期望的相同的事情,但我想知道我自己在这些方面应该使用哪些代码。我的意思是什么是最好的代码。 请告诉我你知道什么。
谢谢。

如果你想要最少的执行时间,你可以测量一下。让我们将每个解决方案运行10000次,看看需要多长时间:

$linefromtext=“句子d.句子e.句子f.”

(Measure命令{1..10000 |%{$mycentenses=$linefromtext-split“(?如果您想要最少的执行时间,您可以测量它。让我们将每个解决方案运行10000次,看看需要多长时间:

$linefromtext=“句子d.句子e.句子f.”

(度量命令{1..10000 |%{$mycentenses=$linefromtext-split“(?“最佳代码”)取决于您未提供的指标。是否希望执行时间最短?源代码字节数最少?可读性最好的代码?内存使用量最少?所有这些指标?如果是,哪个指标应具有哪个权重?根据,您将选择第一个解决方案;-)性能效率如何?CPU性能与内存性能仍然不明确?如果您正在寻找最快的方法,请参阅下面我的答案。“最佳代码”取决于您未提供的指标。是否希望执行时间最短?源代码字节数最少?可读性最好的代码?内存使用量最少?所有这些指标?如果是,哪个指标应具有哪个权重?根据,您将选择第一个解决方案;-)性能效率如何?CPU性能与内存性能仍然模棱两可?如果您正在寻找最快的方法,请参阅下面我的答案。
$mySentences = ($lineFromTheText | Select-String -Pattern "([^.?!]+[.?!])?([^.?!]*$)?" -AllMatches).Matches  | % {$_.Groups[1,2].Value} | % { If (-not ($_ -eq "")) {$_}}