正则表达式 - 重复捕获组 [英] Regex - Repeating Capturing Group

查看:213
本文介绍了正则表达式 - 重复捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图弄清楚如何在以下url字符串中的逗号分隔的值上重复捕获组:

I'm trying to figure out how I can repeat a capture group on the comma-separated values in this the following url string:

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

我正在使用这个 RegExp 这是我想要的返回结果,除了值,因为它们是动态的即。可能是url参数中的2,3,4等用户,并且想知道我是否可以为每个值创建一个捕获组而不是 user1,user2,user3 作为一个捕获-group。

I'm using this RegExp which is return results I want, except for the values since they're dynamic ie. 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.

RegExp: (^ |; | :)(\ w +)=([^; ] +)*

以下是 RegExp

Here is a live demo of it online using RegExp

示例输出:


  • Group1 - (分号,冒号)

  • Group2 - (密钥即.id,名称,城市,邮编)

  • Group3 - (value1)

  • Group4 - (value2)* if exists

  • Group5 - (value3)* if exists

  • Group6 - (value4)* if exists

  • Group1 - (semi-colon,colon)
  • Group2 - (key ie. id,name,city,zip)
  • Group3 - (value1)
  • Group4 - (value2) *if exists
  • Group5 - (value3) *if exists
  • Group6 - (value4) *if exists

etc ...基于我解释的动态值之前。

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?

推荐答案

Reg ex不支持你想要做的事情。当引擎第二次进入捕获组时,它会覆盖第一次捕获的内容。考虑一个简单的例子(感谢 regular-expressions.info ): /(abc | 123)+ / 用于'abc123'。它将匹配abc然后看到加号并重试,匹配123。输出中的最终捕获组将为123。

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'. It will match "abc" then see the plus and try again, matching the "123". The final capturing group in the output will be "123".

无论您尝试何种模式,这种情况都会发生,您设置的任何限制只会在正则表达式接受字符串时更改。考虑 /(abc | 123){2} / 。这接受'abc123',捕获组为123但不是'abc123abc'。将捕获组放入另一组也不起作用。创建捕获组时,就像创建变量一样。它只能有一个值,后续值会覆盖前一个值。你将永远不会拥有比你有括号对更多的捕获组(但你肯定可以少一些)。

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}/. This accepts 'abc123' with the capturing group as "123" but not '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).

一个可能的解决办法就是拆分';'上的字符串,然后是'='上的每个字符串,然后是','上的字符串的右侧。这会让你 [['id','1','2'],['name','user1',...],['city',...],[ 'zip',...]]

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', ...]].

这就是:

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;
}

这篇关于正则表达式 - 重复捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆