[英]PowerShell - regex to get string between two strings
I'm not very experienced in Regex. 我在正则表达方面不是很有经验。 Can you tell me how to get a string value from between two strings?
你能告诉我如何从两个字符串之间获取字符串值吗?
The subject will always be in this format : //subject/some_other_stuff
主题将始终采用以下格式:
//subject/some_other_stuff
I need to get the string found between //
and /
. 我需要在
//
和/
之间找到字符串。
For example: 例如:
Full String = //Manhattan/Project
Full String =
//Manhattan/Project
Output = Manhattan
输出=
Manhattan
Any help will be very much appreciated. 任何帮助将非常感谢。
You can use a negated character class and reference capturing group #1
for your match result. 您可以使用否定字符类并引用捕获组
#1
作为匹配结果。
//([^/]+)/
Explanation: 说明:
// # '//'
( # group and capture to \1:
[^/]+ # any character except: '/' (1 or more times)
) # end of \1
/ # '/'
你可以使用下面使用lookarounds的正则表达式。
(?<=\/\/)[^\/]+(?=\/)
Since the strings are always of the same format, you can simply split them on /
and then retrieve the element at index 2 (the third element): 由于字符串总是具有相同的格式,您可以简单地将它们拆分为
/
然后检索索引2处的元素(第三个元素):
PS > $str = "//Manhattan/Project"
PS > $str.split('/')[2]
Manhattan
PS > $str = "//subject/some_other_stuff"
PS > $str.split('/')[2]
subject
PS >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.