本帖最后由 5i365 于 2022-4-20 09:52 编辑
以前发过类似的贴子, 当时诸多大侠,用|拼接或用正则(?m)的方式匹配到的, 刚刚无意看到一段国外的代码, 感觉很有意思, 既没有用|拼接, 也没有用(?m), 而是用了switch
代码执行后, 成功将===之间的内容提取出来了, 而正则只用了 [=]{3,} 对这个不太理解, 期待高手能指引一下思路, 提前感谢!
另外,如果还是用这种方法, 但是文本内容如下时, 那该怎样用正则取出包含开头和结尾的两行之间的内容?
#Some text
Another text
Even more text with : like that.
==开头==========================
Org: Home Garage Inc.
Author: Mr.Smit
Created: 12/12/2021
Last update: 12/12/2021
Version: 1.1.1
==结尾========================
Other text
========
And More text { Some blocks of code }
Even more not interesting text :-)
- $Content = @'
- #Some text
- Another text
- Even more text with : like that.
-
- ==============================
- Org: Home Garage Inc.
- Author: Mr.Smit
- Created: 12/12/2021
- Last update: 12/12/2021
- Version: 1.1.1
- ==============================
-
- Other text
- And More text { Some blocks of code }
- Even more not interesting text :-)
- '@ -split [Environment]::NewLine
-
- $between = $false
- Switch -regex ($Content)
- {
- '[=]{3,}' {
- $between = !$between
- }
- Default {
- If ($between)
- {
- $_
- }
- }
- }
复制代码
|