正则表达式在斜杠之间查找字符串 [英] Regex to find string between slashes
问题描述
我正在尝试编写一个正则表达式(运气不佳)来捕获出现在较大字符串中特定位置的两个斜杠之间的字符串.
I am trying to write a regex (with very little luck) to capture a string that occurs between two slashes in a specific location within a larger string.
一个示例源字符串是:这里会有一些文本描述,后面跟着路径:/XYZ/String-needed/ABC/ETC
An example source string would be: There will be some text description here followed by the path: /XYZ/String-needed/ABC/ETC
所需的字符串总是在第二个和第三个 /
字符之间.
The string-needed will always be between the second and third /
character.
此正则表达式的上下文在另一个工具中,但它主要模拟正则表达式使用的 Java 上下文.我知道 /
需要转义 \/
但我无法获得代码来查找第二个和第三个实例之间的字符串.
The context of regex for this is within another tool, but it models mostly a java context of regex usage. I know that the /
will need to be escaped \/
but I cannot get the code to find the string between the second and third instance.
推荐答案
您可以使用以下正则表达式:
You can use following regex :
"^\/[^/]+\/([^/]+)\/"
查看演示 https://regex101.com/r/jR9rV5/1
[^/]
是一个 否定字符类 匹配除 /
之外的任何字符串.
[^/]
is a negated character class which match any string except /
.
这篇关于正则表达式在斜杠之间查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!