正则表达式找到的字符串中的最后一个字符或位置
Regex Last Character or Position from Found String
我已经查看了 Regex Last occurrence? but cannot get the regex to work for my example string ("https://www.fakesite.com test one")。我只需要 return 网站名称的最后一个字符(或位置)。我有两个捕获的表达式站点并获取最后一个字符但无法获取表达式以正确查看后面。
(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]
{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|
(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,}) <- Regular Expression for website
(?=.?$). <- Regular Expression for retrieving last character
我一直在使用 https://regex101.com/ 尝试查找,但没有成功。
如何检索最后一个字符或位置?
--编辑--
如何只检索任何字符串的最后一个字符?(在系统工程师中我只需要字母 'r')。 'System Engineer' 是动态的。
"for the position of System Engineer located in"
(?<=position of )(.*)(?= located) <- regex to capture System Engineer between words 'position of' and 'located'
您可以试试下面的正则表达式。下面的正则表达式将检查有效的 url 地址,并为您提供 url.
的最后一个字符
https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}(\w)\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)
上面正则表达式的解释:
https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}
- Matches the http/https://
part of the regex along with www
and the domain name before the first .
.
[a-zA-Z0-9()]{1,6}
- This part matches the last of the url part.
(\w)
- Represents a capturing group capturing the last character of the url. You may use ([a-zA-Z0-9])
manually if you don't want to include _
.
\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)
- Matches the rest part of the url like .uk
or .in
, etc. zero or more times.
你可以在here.
中找到上述正则表达式的演示
参考:匹配有效url的正则表达式取自this答案。
如果您想修改正则表达式;只需在正则表达式后添加 [a-zA-Z]
即可。你可以找到演示 here.
我已经查看了 Regex Last occurrence? but cannot get the regex to work for my example string ("https://www.fakesite.com test one")。我只需要 return 网站名称的最后一个字符(或位置)。我有两个捕获的表达式站点并获取最后一个字符但无法获取表达式以正确查看后面。
(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]
{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|
(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,}) <- Regular Expression for website
(?=.?$). <- Regular Expression for retrieving last character
我一直在使用 https://regex101.com/ 尝试查找,但没有成功。
如何检索最后一个字符或位置?
--编辑--
如何只检索任何字符串的最后一个字符?(在系统工程师中我只需要字母 'r')。 'System Engineer' 是动态的。
"for the position of System Engineer located in"
(?<=position of )(.*)(?= located) <- regex to capture System Engineer between words 'position of' and 'located'
您可以试试下面的正则表达式。下面的正则表达式将检查有效的 url 地址,并为您提供 url.
的最后一个字符https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}(\w)\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)
上面正则表达式的解释:
https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}
- Matches thehttp/https://
part of the regex along withwww
and the domain name before the first.
.
[a-zA-Z0-9()]{1,6}
- This part matches the last of the url part.
(\w)
- Represents a capturing group capturing the last character of the url. You may use([a-zA-Z0-9])
manually if you don't want to include_
.
\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)
- Matches the rest part of the url like.uk
or.in
, etc. zero or more times.
你可以在here.
中找到上述正则表达式的演示参考:匹配有效url的正则表达式取自this答案。
如果您想修改正则表达式;只需在正则表达式后添加 [a-zA-Z]
即可。你可以找到演示 here.