Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
以下是我的字符串: F:\Shared\Common\1a\gruwr050.pdf
F:\Shared\Common\1a\gruwr050.pdf
如何使用正则表达式获取第三个反斜杠之前的字符串?
例如:F:\Shared\Common
F:\Shared\Common
用作^(?:[^\\]*\\){2}[^\\]*图案。
^(?:[^\\]*\\){2}[^\\]*
这是 Python 示例:
>>> re.findall(r'^(?:[^\\]*\\){2}[^\\]*', r'F:\Shared\Common\1a\gruwr050.pdf') ['F:\\Shared\\Common']
Javascript 示例:
'F:\\Shared\\Common\\1a\\gruwr050.pdf'.match(/^(?:[^\\]*\\){2}[^\\]*/) // => ["F:\Shared\Common"]