简体   繁体   English

使用正则表达式在字符串第 n 次出现后捕获

[英]Capturing after the nth occurrence of a string using regex

My test string:我的测试字符串:

/custom-heads/food-drinks/51374-easter-bunny-cake

I am trying to capture the number in the string.我正在尝试捕获字符串中的数字。 The constants in that string are the the number is always preceded by 3 / 's and followed by a - .该字符串中的常量是数字始终以 3 /开头并后跟-

I am a regex noob and am struggling with this.我是一个正则表达式新手,正在为此苦苦挣扎。 I cobbled together (\/)(.*?)(-) and then figured I could get the last one programmatically, but I would really like to understand regex better and would love if someone could show me the regex to get the last occurrence of numbers between / and - .我拼凑(\/)(.*?)(-)然后想我可以通过编程方式获得最后一个,但我真的很想更好地理解正则表达式并且如果有人可以向我展示正则表达式以获得最后一次出现我会很高兴/-之间的数字。

Don't use regexes if possible, i reccomend you to read - https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/ blog post尽可能不要使用正则表达式,我建议您阅读 - https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/博客文章

To your question, its easier, faster, more bullet proof to get it using splits对于您的问题,使用拆分更容易、更快、更防弹

const articleName = "/custom-heads/food-drinks/51374-easter-bunny-cake".split("/")[3]
// '51374-easter-bunny-cake'

const articleId = articleName.split("-")[0]

// '51374'

hope it helps希望能帮助到你

You may use this regex with a capture group:您可以将此正则表达式与捕获组一起使用:

^(?:[^\/]*\/){3}([^-]+)

Or in modern browsers you can use lookbehind assertion:或者在现代浏览器中,您可以使用后向断言:

/(?<=^(?:[^\/]*\/){3})[^-]+/

RegEx Demo 1正则表达式演示 1

RegEx Demo 2正则表达式演示 2

RegEx Code:正则表达式代码:

  • ^ : Start ^ : 开始
  • (?:[^\/]*\/){3} : Match 0 or more non- / characters followed by a / . (?:[^\/]*\/){3} :匹配 0 个或多个非/字符后跟/ Repeat this group 3 times重复此组3次
  • ([^-]+) : Match 1+ of non-hyphen characters ([^-]+) :匹配 1+ 个非连字符

Code:代码:

 const s = `/custom-heads/food-drinks/51374-easter-bunny-cake`; const re = /^(?:[^\/]*\/){3}([^-]+)/; console.log (s.match(re)[1]);

Use利用

 const str = `/custom-heads/food-drinks/51374-easter-bunny-cake` const p = /(?:\/[^\/]*){2}\/(\d+)-/ console.log(str.match(p)?.[1])

See regex proof .请参阅正则表达式证明

EXPLANATION解释

Non-capturing group (?:\/[^\/]*){2}
{2} matches the previous token exactly 2 times
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
  Match a single character not present in the list below [^\/]
  * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
  \/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
1st Capturing Group (\d+)
  \d matches a digit (equivalent to [0-9])
  + matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)

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

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