Powershell:从字符串中提取文本。

时间:2022-09-13 13:30:21

How do I extract the "program name" from a string. The string will look like this :

如何从字符串中提取“程序名”。弦看起来是这样的:

% O0033(SUB RAD MSD 50R III) G91G1X-6.4Z-2.F500 G3I6.4Z-8. G3I6.4 G3R3.2X6.4F500 G91G0Z5. G91G1X-10.4 G3I10.4 G3R5.2X10.4 G90G0Z2. M99 %

% O0033(SUB RAD MSD 50R III) G91G1X-6.4Z-2。F500 G3I6.4Z-8。G3I6.4 G3R3.2X6.4F500 G91G0Z5。g91g1x - 10.4 G3I10.4 G3R5.2X10.4 G90G0Z2。M99 %

The program name is (SUB RAD MSD 50R III). Storing the result in another string is fine. I'm learning powershell so any explaination of your answers will be appreciated.

程序名是(SUB RAD MSD 50R III)。将结果存储在另一个字符串中是可以的。我正在学习powershell,所以你的答案的任何解释都将被欣赏。

4 个解决方案

#1


39  

The following regex extract anything between the parenthesis:

下面的regex将在括号之间提取任何内容:

PS> $prog = [regex]::match($s,'\(([^\)]+)\)').Groups[1].Value
PS> $prog
SUB RAD MSD 50R III


Explanation (created with RegexBuddy)

Match the character '(' literally «\(»
Match the regular expression below and capture its match into backreference number 1 «([^\)]+)»
   Match any character that is NOT a ) character «[^\)]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character ')' literally «\)»

Check these links:

检查这些链接:

http://www.regular-expressions.info

http://www.regular-expressions.info

http://powershell.com/cs/blogs/tobias/archive/2011/10/27/regular-expressions-are-your-friend-part-1.aspx

http://powershell.com/cs/blogs/tobias/archive/2011/10/27/regular——表达——-你的朋友- 1. aspx一部分

http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular-expressions-are-your-friend-part-2.aspx

http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular——表达——-你的朋友- 2. aspx一部分

http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular-expressions-are-your-friend-part-3.aspx

http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular——表达——-你的朋友- 3. aspx一部分

#2


15  

If program name is always the first thing in (), and doesn't contain other )s than the one at end, then $yourstring -match "[(][^)]+[)]" does the matching, result will be in $Matches[0]

如果程序名总是首先在(),和不包含其他)年代比结束,那么美元yourstring匹配”[(][^)]+[)]”是否匹配,结果将在$ Matches[0]

#3


5  

Just to add a non-regex solution:

添加一个非regex解决方案:

'(' + $myString.Split('()')[1] + ')'

This splits the string at the parentheses and takes the string from the array with the program name in it.

这将在括号处分割字符串,并从数组中获取包含程序名的字符串。

If you don't need the parentheses, just use:

如果你不需要圆括号,就用:

$myString.Split('()')[1]

#4


1  

Using -replace

使用替换

 $string = '% O0033(SUB RAD MSD 50R III) G91G1X-6.4Z-2.F500 G3I6.4Z-8.G3I6.4 G3R3.2X6.4F500 G91G0Z5. G91G1X-10.4 G3I10.4 G3R5.2X10.4 G90G0Z2. M99 %'
 $program = $string -replace '^%\sO\d{4}\((.+?)\).+$','$1'
 $program

SUB RAD MSD 50R III

#1


39  

The following regex extract anything between the parenthesis:

下面的regex将在括号之间提取任何内容:

PS> $prog = [regex]::match($s,'\(([^\)]+)\)').Groups[1].Value
PS> $prog
SUB RAD MSD 50R III


Explanation (created with RegexBuddy)

Match the character '(' literally «\(»
Match the regular expression below and capture its match into backreference number 1 «([^\)]+)»
   Match any character that is NOT a ) character «[^\)]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character ')' literally «\)»

Check these links:

检查这些链接:

http://www.regular-expressions.info

http://www.regular-expressions.info

http://powershell.com/cs/blogs/tobias/archive/2011/10/27/regular-expressions-are-your-friend-part-1.aspx

http://powershell.com/cs/blogs/tobias/archive/2011/10/27/regular——表达——-你的朋友- 1. aspx一部分

http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular-expressions-are-your-friend-part-2.aspx

http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular——表达——-你的朋友- 2. aspx一部分

http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular-expressions-are-your-friend-part-3.aspx

http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular——表达——-你的朋友- 3. aspx一部分

#2


15  

If program name is always the first thing in (), and doesn't contain other )s than the one at end, then $yourstring -match "[(][^)]+[)]" does the matching, result will be in $Matches[0]

如果程序名总是首先在(),和不包含其他)年代比结束,那么美元yourstring匹配”[(][^)]+[)]”是否匹配,结果将在$ Matches[0]

#3


5  

Just to add a non-regex solution:

添加一个非regex解决方案:

'(' + $myString.Split('()')[1] + ')'

This splits the string at the parentheses and takes the string from the array with the program name in it.

这将在括号处分割字符串,并从数组中获取包含程序名的字符串。

If you don't need the parentheses, just use:

如果你不需要圆括号,就用:

$myString.Split('()')[1]

#4


1  

Using -replace

使用替换

 $string = '% O0033(SUB RAD MSD 50R III) G91G1X-6.4Z-2.F500 G3I6.4Z-8.G3I6.4 G3R3.2X6.4F500 G91G0Z5. G91G1X-10.4 G3I10.4 G3R5.2X10.4 G90G0Z2. M99 %'
 $program = $string -replace '^%\sO\d{4}\((.+?)\).+$','$1'
 $program

SUB RAD MSD 50R III