C#取括号里面的值,正则表达式怎么写

作者&投稿:夕史 (若有异议请与网页底部的电邮联系)
c#用正则表达式提取小括号中的内容~

给表达式加一个别名如下所示,就可以很简单的取到你想要的字符串了。
Regex rex = new Regex("(?(.*))");String str = rex.Match("aaaa(bbbbbb)jlkoihj").Groups["MYSTR"].ToString();// 结果 (bbbbbb)如不清楚可以Hi我。希望能帮到你!
/* 2016-11-17 更新 留言的朋友说取不到括号中的内容,更新下,如果一定要的是不包含括号的内容,变更下表达式就可以了。 */ rex = new Regex("((?\\w+))");String str1 = rex.Match("aaaa(bbbbbb)jlkoihj").Groups["MYSTR"].ToString();// 结果 bbbbbb

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;//引入命名空间

namespace testRex
{
class Program
{
static void Main(string[] args)
{
string text = "[[字符串1|字符串2]]";
string pattern = "\\[\\[(.*?)\\|(.*?)\\]\\]";//正则,[]|都是保留字符,所以要转义,.net \本身是转义字符,所以要用\\
MatchCollection mc = Regex.Matches(text, pattern);
Console.WriteLine("Number of matches found : {0}", mc.Count);

// Enumerated all the matches.
foreach (Match m in mc)
{
Console.WriteLine("Sentence containing John as 7th word: {0}", m.Groups[1].Value);
}
Console.ReadLine();
}
}
}

Regex reg = new Regex(@"\(([^)]*)\)");
Match m = reg.Match(str);
if(m.Success)
Response.Write(m.Result("$1"));

string str= "abc(123)";
Regex reg = new Regex(@"str=|\d{3}");
Match m = reg.Match(str);
Response.Write(m.Value);//此值便是你想要的:取出 str=123;

string str = "absd(123)";
MatchCollection _MatchCollection = Regex.Matches(str, ".*?(?<num>[0-9]{1,}).*?", RegexOptions.IgnoreCase);
foreach (Match item in _MatchCollection)
{
MessageBox.Show(item.Groups["num"].Value);
}


离石区13763292649: C#取括号里面的值,正则表达式怎么写 -
索坚苓苾: string str= "abc(123)"; Regex reg = new Regex(@"str=|\d{3}"); Match m = reg.Match(str); Response.Write(m.Value);//此值便是你想要的:取出 str=123;

离石区13763292649: c#用正则表达式提取小括号中的内容 -
索坚苓苾: 给表达式加一个别名如下所示,就可以很简单的取到你想要的字符串了.123 Regex rex = newRegex("(?<MYSTR>(.*))"); String str = rex.Match("aaaa(bbbbbb)jlkoihj").Groups["MYSTR"].ToString();// 结果 (bbbbbb) 如不清楚可以Hi...

离石区13763292649: C# 正则表达式 括号内的东西取出 -
索坚苓苾: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions;//引入命名空间 namespace testRex { class Program { static void Main(string[] args) { string text = "[[字符串1|字符串...

离石区13763292649: C# 求正则表达式写法 -
索坚苓苾: 先取出一个括号的内容 String str = Regex.Match("*{**|**|**}*", "(?<={)[^}]+").Value 之后进行3个判断:1.str中没有 "||"2.str首尾字符不是"|"3.str中有3个"|"

离石区13763292649: 如何用正则表达式匹配括号中的内容. -
索坚苓苾: “()” 等这些在正则表达式中有特殊意义的字符,要当普通字符使用时,在其前面加'\'即可. 正则表达式中的圆括号的作用: 1. 正则表达式中的圆括号的作用是对字符进行分组,并保存匹配的文本. 2. 圆括号用法I:对字符或元字符进行分组,...

离石区13763292649: c#利用正则表达式取出括号中的内容,可能会有多个括号重叠
索坚苓苾: 不涉及到括号嵌套这样就可以取出来了. var regex = new System.Text.RegularExpressions.Regex(@"\((?&lt;v&gt;.*?)\)"); var v = regex.Match("(abc)cd(deg)xsw(sxws)").Groups["v"]; Console.Write(v);

离石区13763292649: C#如何使用正则表达式提取超链接中的文字部分?就是<a>文字部分</a>中间的文字. -
索坚苓苾: string html = 要匹配的字符串; Regex reg = new Regex(@"<a\s*[^>]*>([\s\S]+?)</a>", RegexOptions.IgnoreCase); Match m = reg.Match(html); while(m.IsSuccess){ string innerHTML = m.Result("$1");// 得到正则的括号里的内容,就是a的...

离石区13763292649: 怎么用正则表达式匹配中括号内的字符 -
索坚苓苾: 要看你内容里是否有中括号的,如果没有,那就简单,用正则 \[([^\[\]]*)\] 这样就可以把括号内的内容匹配到$1分组里了,怎么取$1要看你用的是什么编程语言了

离石区13763292649: C#正则表达式括号比配 -
索坚苓苾: string str = "";//原字符串 Regex reg = new Regex(@"\(A.+?\)", RegexOptions.Singleline); Match mat = reg.Match(str); while(mat.Success) { Response.Write(mat.Value); mat = reg.Match(str, mat.Index+mat.Length); }

离石区13763292649: 正则表达式括号间的字符串匹配
索坚苓苾: C# 语言: string s = "(1,2,3,4) abd (3,5,7,1)"; s = Regex.Replace(s, @"\(([^)]*)\)", "$1"); $1表示匹配的结果

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网