Regex 如何查找所有出现的多个字符串?
我有一个示例字符串:Regex 如何查找所有出现的多个字符串?,regex,Regex,我有一个示例字符串: There are from {d} to {d} {s} balls available. Average of {f} balls. 我想找到所有出现的标签:{d},{f}和{s} 正则表达式模式是什么?使用以下带有全局修饰符的正则表达式: /\{[dfs]\}/g 见演示 [dfs]是一个字符类,将匹配其中的一个字符 使用以下带有全局修饰符的正则表达式: /\{[dfs]\}/g 见演示 [dfs]是一个字符类,将匹配其中的一个字符 使用以下带有全局修饰符的正则
There are from {d} to {d} {s} balls available. Average of {f} balls.
我想找到所有出现的标签:{d}
,{f}
和{s}
正则表达式模式是什么?使用以下带有全局修饰符的正则表达式:
/\{[dfs]\}/g
见演示
[dfs]
是一个字符类,将匹配其中的一个字符 使用以下带有全局修饰符的正则表达式:
/\{[dfs]\}/g
见演示
[dfs]
是一个字符类,将匹配其中的一个字符 使用以下带有全局修饰符的正则表达式:
/\{[dfs]\}/g
见演示
[dfs]
是一个字符类,将匹配其中的一个字符 使用以下带有全局修饰符的正则表达式:
/\{[dfs]\}/g
见演示
[dfs]
是一个字符类,将匹配其中的一个字符 我想这样做
\{[dsf]\}
,\{
匹配文字\}
,{
符号}
字符类,它匹配给定列表中的单个字符,即[dsf]
或d
或s
f
- 我想这样做
\{[dsf]\}
,\{
匹配文字\}
,{
符号}
字符类,它匹配给定列表中的单个字符,即[dsf]
或d
或s
f
- 我想这样做
\{[dsf]\}
,\{
匹配文字\}
,{
符号}
字符类,它匹配给定列表中的单个字符,即[dsf]
或d
或s
f
- 我想这样做
\{[dsf]\}
,\{
匹配文字\}
,{
符号}
字符类,它匹配给定列表中的单个字符,即[dsf]
或d
或s
f
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"There are from 5 to 7 red balls available. Average of 10.3 balls.\n" +
"There are from 3 to 6 blue balls available. Average of 3.5 balls.\n" +
"There are from 4 to 7 green balls available. Average of 5.6 balls.\n" +
"There are from 6 to 8 yellow balls available. Average of 16.7 balls.\n" +
"There are from 4 to 9 purple balls available. Average of 10.4 balls.\n" +
"There are from 6 to 8 orange balls available. Average of 17.6 balls.\n" +
"There are from 7 to 7 white balls available. Average of 18.6 balls.\n" +
"There are from 4 to 6 black balls available. Average of 3.7 balls.\n" +
"There are from 5 to 9 tan balls available. Average of 5.6 balls.\n";
string pattern = @"(?'min'\d+) to (?'max'\d+) (?'color'\w+)[^\d]+(?'average'\d+\.\d+)";
Regex expr = new Regex(pattern);
MatchCollection matches = expr.Matches(input);
List<string> results = new List<string>();
foreach (Match match in matches)
{
string CSV = string.Join(",", new string[] {
match.Groups["min"].Value, match.Groups["max"].Value, match.Groups["color"].Value, match.Groups["average"].Value
});
results.Add(CSV);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
“有5到7个红色球可用。平均10.3个球。\n”+
“有3到6个蓝球可用。平均3.5个球。\n”+
“有4到7个绿色球可用。平均5.6个球。\n”+
“有6到8个黄色球可用。平均16.7个球。\n”+
“有4到9个紫色球可用。平均10.4个球。\n”+
“有6到8个橙色球可用。平均17.6个球。\n”+
“有7到7个白色球可用。平均18.6个球。\n”+
“有4到6个黑色球可用。平均3.7个球。\n”+
“有5到9个棕褐色球可用。平均5.6个球。\n”;
字符串模式=@“(?'min'\d+)到(?'max'\d+('color'\w+)[^\d]+(?'average'\d+\.\d+)”;
正则表达式expr=新正则表达式(模式);
MatchCollection matches=expr.matches(输入);
列表结果=新列表();
foreach(匹配中的匹配)
{
字符串CSV=string.Join(“,”,新字符串[]{
match.Groups[“min”].值,match.Groups[“max”].值,match.Groups[“color”].值,match.Groups[“average”]值
});
结果。添加(CSV);
}
}
}
}
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"There are from 5 to 7 red balls available. Average of 10.3 balls.\n" +
"There are from 3 to 6 blue balls available. Average of 3.5 balls.\n" +
"There are from 4 to 7 green balls available. Average of 5.6 balls.\n" +
"There are from 6 to 8 yellow balls available. Average of 16.7 balls.\n" +
"There are from 4 to 9 purple balls available. Average of 10.4 balls.\n" +
"There are from 6 to 8 orange balls available. Average of 17.6 balls.\n" +
"There are from 7 to 7 white balls available. Average of 18.6 balls.\n" +
"There are from 4 to 6 black balls available. Average of 3.7 balls.\n" +
"There are from 5 to 9 tan balls available. Average of 5.6 balls.\n";
string pattern = @"(?'min'\d+) to (?'max'\d+) (?'color'\w+)[^\d]+(?'average'\d+\.\d+)";
Regex expr = new Regex(pattern);
MatchCollection matches = expr.Matches(input);
List<string> results = new List<string>();
foreach (Match match in matches)
{
string CSV = string.Join(",", new string[] {
match.Groups["min"].Value, match.Groups["max"].Value, match.Groups["color"].Value, match.Groups["average"].Value
});
results.Add(CSV);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
“有5到7个红色球可用。平均10.3个球。\n”+
“有3到6个蓝球可用。平均3.5个球。\n”+
“有4到7个绿色球可用。平均5.6个球。\n”+
“有6到8个黄色球可用。平均16.7个球。\n”+
“有4到9个紫色球可用。平均10.4个球。\n”+
“有6到8个橙色球可用。平均17.6个球。\n”+
“有7到7个白色球可用。平均18.6个球。\n”+
“有4到6个黑色球可用。平均3.7个球。\n”+
“有5到9个棕褐色球可用。平均5.6个球。\n”;
字符串模式=@“(?'min'\d+)到(?'max'\d+('color'\w+)[^\d]+(?'average'\d+\.\d+)”;
正则表达式expr=新正则表达式(模式);
MatchCollection matches=expr.matches(输入);
列表结果=新列表();
foreach(匹配中的匹配)
{
字符串CSV=string.Join(“,”,新字符串[]{
match.Groups[“min”].值,match.Groups[“max”].值,match.Groups[“color”].值,match.Groups[“average”]值
});
结果。添加(CSV);
}
}
}
}
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"There are from 5 to 7 red balls available. Average of 10.3 balls.\n" +
"There are from 3 to 6 blue balls available. Average of 3.5 balls.\n" +
"There are from 4 to 7 green balls available. Average of 5.6 balls.\n" +
"There are from 6 to 8 yellow balls available. Average of 16.7 balls.\n" +
"There are from 4 to 9 purple balls available. Average of 10.4 balls.\n" +
"There are from 6 to 8 orange balls available. Average of 17.6 balls.\n" +
"There are from 7 to 7 white balls available. Average of 18.6 balls.\n" +
"There are from 4 to 6 black balls available. Average of 3.7 balls.\n" +
"There are from 5 to 9 tan balls available. Average of 5.6 balls.\n";
string pattern = @"(?'min'\d+) to (?'max'\d+) (?'color'\w+)[^\d]+(?'average'\d+\.\d+)";
Regex expr = new Regex(pattern);
MatchCollection matches = expr.Matches(input);
List<string> results = new List<string>();
foreach (Match match in matches)
{
string CSV = string.Join(",", new string[] {
match.Groups["min"].Value, match.Groups["max"].Value, match.Groups["color"].Value, match.Groups["average"].Value
});
results.Add(CSV);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
“有5到7个红色球可用。平均10.3个球。\n”+
“有3到6个蓝球可用。平均3.5个球。\n”+
“有4到7个绿色球可用。平均5.6个球。\n”+