扩展[^,] +,C#中的正则表达式 [英] Extending [^,]+, Regular Expression in C#

查看:140
本文介绍了扩展[^,] +,C#中的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


用于c#中的变量声明和初始化的正则表达式

Regex for variable declaration and initialization in c#

正则表达式来解析CSV值,我遇到了这个正则表达式

I was looking for a Regular Expression to parse CSV values, and I came across this Regular Expression

[^,]+

我的工作是通过在每次出现,时分割单词。我想知道的是我有字符串

Which does my work by splitting the words on every occurance of a ",". What i want to know is say I have the string


value_name v1,v2,v3,v4,...

value_name v1,v2,v3,v4,...

现在我想要一个正则表达式来找到我的单词v1,v2,v3,v4 ..

Now I want a regular expression to find me the words v1,v2,v3,v4..

我试过 - >

^value_name\s+([^,]+)*

但它没有为我工作。你能告诉我我做错了什么吗?我记得工作正则表达式和他们的statemachine实现。它不工作在同样的方式。

But it didn't work for me. Can you tell me what I am doing wrong? I remember working on regular expressions and their statemachine implementation. Doesn't it work in the same way.

如果字符串以Value_name开头,后跟一个或多个空格。转到下一状态。在那个国家读一个字直到,来了。然后再做!

If a string starts with Value_name followed by one or more whitespaces. Go to Next State. In That State read a word until a "," comes. Then do it again! And each word will be grouped!

我理解错了吗?

推荐答案

您可以使用类似于建议的正则表达式:

You could use a Regex similar to those proposed:

(?:^value_name\s+)?([^,]+)(?:\s*,\s*)?




  • 第一组是非捕获,开始和 value_name

    为确保正则表达式在所有匹配项中仍然有效,我们使用'?'已修改(表示最多匹配一次)。

    • The first group is non-capturing and would match the start of the line and the value_name.
      To ensure that the Regex is still valid over all matches, we make that group optional by using the '?' modified (meaning match at most once).

      第二组正在捕获并与您的 vXX 数据匹配。

      The second group is capturing and would match your vXX data.

      第三组是非捕获,并且匹配以及它之前和之后的任何空格。

      再次,我们通过使用'?'修饰符使其可选,否则最后的'vXX'不匹配,除非我们以最后的','结束字符串。

      The third group is non-capturing and would match the ,, and any whitespace before and after it.
      Again, we make it optional by using the '?' modifier, otherwise the last 'vXX' group would not match unless we ended the string with a final ','.

      在试用中,Regex不会多次匹配:你必须记住,如果你想要一个正则表达式匹配字符串中的多次出现,整个Regex需要匹配每次出现在字符串中,所以你必须构建你的Regex不仅匹配字符串'value_name'的开头,而且匹配每次出现的

      In you trials, the Regex wouldn't match multiple times: you have to remember that if you want a Regex to match multiple occurrences in a strings, the whole Regex needs to match every single occurrence in the string, so you have to build your Regex not only to match the start of the string 'value_name', but also match every occurrence of 'vXX' in it.

      在C#中,您可以使用以下代码列出所有匹配项和组:

      In C#, you could list all matches and groups using code like this:

      Regex r = new Regex(@"(?:^value_name\s+)?([^,]+)(?:\s*,\s*)?");
      Match m = r.Match(subjectString);
      while (m.Success) {
          for (int i = 1; i < m.Groups.Count; i++) {
              Group g = m.Groups[i];
              if (g.Success) {
                  // matched text: g.Value
                  // match start: g.Index
                  // match length: g.Length
              } 
          }
          m = m.NextMatch();
      } 
      

      这篇关于扩展[^,] +,C#中的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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