简体   繁体   English

正则表达式获取两个字符串之间的字符串

[英]Regex to get a string between two strings

Anyone please help in getting the regex to get the string between two strings.任何人都请帮助获取正则表达式以获取两个字符串之间的字符串。

Full string:完整字符串:

image001.jpg (1.69 KB)SpinTel - 372464.pdf (270.90 KB)SpinTel - 372464.xlsm (113.08 KB)

Output expected: SpinTel - 372464.pdf & SpinTel - 372464.xlsm Output 预期: SpinTel - 372464.pdf & SpinTel - 372464.xlsm

I tried the expression:我试过表达:

((?i)Spintel)(.*)(.pdf)

and got the one output successfully,并成功拿到output,

but when I changed the expression to但是当我将表达式更改为

((?i)Spintel)(.*)(.xlsm)

I am getting the output as SpinTel - 372464.pdf (270.90 KB)SpinTel - 372464.xlsm .我得到 output 作为SpinTel - 372464.pdf (270.90 KB)SpinTel - 372464.xlsm My expected output in this is SpinTel - 372464.xlsm .我预期的 output 是SpinTel - 372464.xlsm

Please help.请帮忙。

Thanks in advance.提前致谢。


To get a match only:仅获取匹配项:

\bSpinTel\b.*?\.(?:pdf|xlsm)\b
  • \bSpinTel\b Match SpinTel between word boundaries to prevent a partial match \bSpinTel\b在单词边界之间匹配 SpinTel 以防止部分匹配
  • .*? Match as least as possible chars匹配尽可能少的字符
  • \.(?:pdf|xlsm)\b match a dot and either pfd or xlsm followed by a word boundary \.(?:pdf|xlsm)\b匹配点和 pfd 或 xlsm 后跟单词边界

See a regex demo查看正则表达式演示

If it should be followed by parenthesis, you can use a capture group for the SpinTel part, and match the parenthesis after it.如果后面应该有括号,您可以为 SpinTel 部分使用捕获组,并匹配其后的括号。

(\bSpinTel\b.*?\.(?:pdf|xlsm)\s+)\([^()]+\)

See another regex demo查看另一个正则表达式演示

To get matches and be prepared for other file extensions.获取匹配项并为其他文件扩展名做好准备。

((?i)Spintel - \d*.\w*)

Output Output

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM