有一个正则表达式可以做我想做的事情,但是我希望它能够只检查字符串的开头,而不看后面的内容。
工作正则表达式:
[RegEx]$RegEx = "(.+?) (.+?)-(.+?)-(.+?)$"
失败:
[RegEx]$RegEx = "^((.+?) (.+?)-(.+?)-(.+?))"
[RegEx]$RegEx = "\A(.+?) (.+?)-(.+?)-(.+?)"
示例:
# Ok:
BEL Green-Fruit-Appel Stuff
BEL Green-Fruit-Appel Other stuff stuff
# Not ok (without anything):
BEL Green-Fruit-Appel
谢谢你的帮助。
发布于 2014-08-11 16:00:32
抛弃$
并使用^
C:\PS> 'BEL Green-Fruit-Appel' -match '^(.+?) (.+?)-(.+?)-(.+?)'
True
C:\PS> $matches
Name Value
---- -----
4 A
3 Fruit
2 Green
1 BEL
0 BEL Green-Fruit-A
最后一个捕获组只是A
,因为您使用的是非贪婪的?
,所以它在第一个字符之后停止。如果要使用整个单词,请将最后一个捕获组更改为‘-(\w+)’。
https://stackoverflow.com/questions/25240656
复制相似问题