简体   繁体   English

正则表达式 - 重复捕获组

[英]Regex - Repeating Capturing Group

I'm trying to figure out how I can repeat a capture group on the comma-separated values in this the following url string:我试图弄清楚如何在以下 url 字符串中的comma-separated值上重复捕获组:

id=1,2;name=user1,user2,user3;city=Oakland,San Francisco,Seattle;zip=94553,94523;

I'm using this RegExp which is return results I want, except for the values since they're dynamic ie.我正在使用这个RegExp ,它是我想要的返回结果,但值除外,因为它们是动态的,即。 could be 2,3,4,etc users in the url parameter and was wondering if I could create a capture group for each value instead of user1,user2,user3 as one capture-group. url 参数中可能有 2、3、4 等用户,我想知道是否可以为每个值创建一个捕获组,而不是将user1,user2,user3作为一个捕获组。

RegExp: (^|;|:)(\\w+)=([^;]+)*正则表达式: (^|;|:)(\\w+)=([^;]+)*

Here is a live demo of it online using RegExp这是使用RegExp在线进行的现场演示

Example Output:示例输出:

  • Group1 - (semi-colon,colon) Group1 - (分号,冒号)
  • Group2 - (key ie. id,name,city,zip) Group2 - (key ie. id,name,city,zip)
  • Group3 - (value1)组 3 -(值 1)
  • Group4 - (value2) *if exists Group4 - (value2) *如果存在
  • Group5 - (value3) *if exists Group5 - (value3) *如果存在
  • Group6 - (value4) *if exists Group6 - (value4) *如果存在

etc... based on the dynamic values like I explained before.等等...基于我之前解释的动态值。

Question: Whats wrong with my expression I'm using the * to loop for repeated patterns?问题:我的表达式有什么问题我使用*来循环重复模式?

Regex doesn't support what you're trying to do.正则表达式不支持您尝试执行的操作。 When the engine enters the capturing group a second time, it overwrites what it had captured the first time.当引擎第二次进入捕获组时,它会覆盖第一次捕获的内容。 Consider a simple example (thanks regular-expressions.info ): /(abc|123)+/ used on 'abc123' .考虑一个简单的例子(感谢正则表达式.info ): /(abc|123)+/用于'abc123' It will match "abc" then see the plus and try again, matching the "123".它将匹配“abc”,然后看到加号并重试,匹配“123”。 The final capturing group in the output will be "123".输出中的最终捕获组将为“123”。

This happens no matter what pattern you try and any limitation you set simply changes when the regex will accept the string.无论您尝试哪种模式,当正则表达式接受字符串时,您设置的任何限制都会发生这种情况。 Consider /(abc|123){2}/ .考虑/(abc|123){2}/ This accepts 'abc123' with the capturing group as "123" but not 'abc123abc'.这接受带有捕获组的“abc123”为“123”,但不接受“abc123abc”。 Putting a capturing group inside another doesn't work either.将一个捕获组放入另一个组中也不起作用。 When you create a capturing group, it's like creating a variable.创建捕获组时,就像创建变量一样。 It can only have one value and subsequent values overwrite the previous one.它只能有一个值,后续值会覆盖前一个值。 You'll never be able to have more capturing groups than you have parentheses pairs (you can definitely have fewer, though).您永远无法拥有比括号对更多的捕获组(不过,您绝对可以拥有更少的捕获组)。

A possible fix then would be to split the string on ';', then each of those on '=', then the right-hand side of those on ','.一个可能的解决方法是在 ';' 上拆分字符串,然后是在 '=' 上的每个字符串,然后是在 ',' 上的字符串的右侧。 That would get you [['id', '1', '2'], ['name', 'user1', ...], ['city', ...], ['zip', ...]] .那会让你[['id', '1', '2'], ['name', 'user1', ...], ['city', ...], ['zip', ...]]

That comes out to be:结果是:

function (str) {
  var afterSplit = str.split(';|:');
  afterSplit.pop() // final semicolon creates empty string
  for (var i = 0; i < afterSplit.length; i++) {
    afterSplit[i] = afterSplit[i].split('=');
    afterSplit[i][1] = afterSplit[i][1].split(','); // optionally, you can flatten the array from here to get something nicer
  }
  return afterSplit;
}

Capturing Group Repeated重复捕获组

String: !abc123def!字符串:!abc123def! regex: /!((abc|123|def)+)!/正则表达式:/!((abc|123|def)+)!/

Matchs:比赛:

Group 1: abc123def第 1 组:abc123def

Group 2: def第 2 组:定义

source: https://www.regular-expressions.info/captureall.html来源: https : //www.regular-expressions.info/captureall.html

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

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