Tag: regex

正则表达式解析函数和参数

我有以下字符串: function(param1, param2, param3, param4, …, paramN) 我需要一个正则表达式代码来解析它作为一个字符串数组: function param1 param2 param3 param4 . . . paramN 我在网上尝试了几个例子,但似乎没有一个适合我。 [编辑] 没有和这个建议打交道。 检查以上代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace TestRegex { class Program { static void Main(string[] args) { string Expression = String.Empty; while (Expression.Trim() != “EXIT”) { Expression = […]

使用正则表达式匹配任何字符,直到达到子字符串?

我希望能够匹配特定的字符序列,从特定的子字符串开始,以特定的子字符串结束。 如果在一行上只有一个匹配的实例,那么我的正向前瞻性正则表达式会起作用,但如果一行上应该存在多个匹配则不行。 我理解这是因为(。+)捕获所有内容,直到找到最后一个正向前瞻表达式。 如果它能捕获所有内容直到找到第一个表达式,那就太好了。 这是我的正则表达式尝试: @@FOO\[(.*)(?=~~)~~(.*)(?=\]@@)\]@@ 样本输入: @@FOO[abc~~hi]@@ @@FOO[def~~hey]@@ 期望的输出:2个匹配,每个具有2个匹配的组(abc,hi)和(def,嘿)。 实际输出:1与2组比赛(abc ~~ hi)@@ @@ FOO [def,嘿] 有没有办法获得所需的输出? 提前致谢!

c#regex – 从类文件(.cs)中选择类属性名称,方法名称和字段

我想匹配(从类文件中选择)methodsname,属性名称和字段名称。 这是示例类: class Perl { string _name; public string Name { get; set; } public Perl() { // Assign this._name this._name = “Perl”; // Assign _name _name = “Sam”; // The two forms reference the same field. Console.WriteLine(this._name); Console.WriteLine(_name); } public static string doSomething(string test) { bla test; } } 我得到了方法的代码: (?:public|private|protected)([\s\w]*)\s+(\w+)\s*\(\s*(?:\w+\s+(\w+)\s*,?\s*)+\) 我有问题: 以上正则表达式代码获取所有方法,它工作得很好,但我也希望它选择方法名称,但没有参数和访问器。 所以从exaplmce类使用我的代码结果将是: […]

在目录c#中查找具有匹配模式的文件?

string fileName = “”; string sourcePath = @”C:\vish”; string targetPath = @”C:\SR”; string sourceFile = System.IO.Path.Combine(sourcePath, fileName); string destFile = System.IO.Path.Combine(targetPath, fileName); string pattern = @”23456780″; var matches = Directory.GetFiles(@”c:\vish”) .Where(path => Regex.Match(path, pattern).Success); foreach (string file in matches) { Console.WriteLine(file); fileName = System.IO.Path.GetFileName(file); Console.WriteLine(fileName); destFile = System.IO.Path.Combine(targetPath, fileName); System.IO.File.Copy(file, destFile, true); } 我的上述程序适用于单一模式。 我正在使用上面的程序来查找具有匹配模式的目录中的文件但在我的情况下我有多个模式所以我需要将string […]

System.Net.MailMessage允许一些无效的电子邮件地址格式

许多人可能已经意识到,正确validation电子邮件地址可能有点噩梦。 您可以整天搜索与当前RFC标准匹配的C#正则表达式,并且您将找到可以提供不同结果的不同正则表达式。 如果查看http://en.wikipedia.org/wiki/Email_address#Local_part ,您将看到不允许在本地部分的开头或结尾处出现句点。 也不允许连续两个时期。 但是,以下NUnit测试certificateSystem.Net.MailMessage允许您为某些无效的电子邮件地址格式实例化MailMessage对象。 [Test] [TestCase(@”foobar@exampleserver”)] //technically valid from the wiki article [TestCase(@”jsmith@[192.168.2.1]”)] //technically valid from the wiki article [TestCase(@”niceandsimple@example.com”)] //vanilla email address [TestCase(@”very.common@example.com”)] //also standard [TestCase(@”a.little.lengthy.but.fine@dept.example.com”)] //long with lots of periods [TestCase(@”disposable.style.email.with+symbol@example.com”)] //disposable with the + symbol [TestCase(@”other.email-with-dash@example.com”)] //period and dash in local part [TestCase(@”user-test-hyphens@example-domain.com”)] //lots of hyphens [TestCase(@”!#$%&’*+-/=?^_`{|}~@example-domain.com”)] //all these symbols […]

图像url的正则表达式

我已经使用HtmlAgilityPack解析页面,并获得大多数img源代码。 然而,许多网站在img src属性以外的地方包含img url(例如内联javascript,不同的属性,不同的元素)。 我想投一个稍微宽一点的网并在整个html字符串上运行一个正则表达式,在正则表达式中捕获以下内容。 必须以http://,https://,//或/开头 然后,任意数量的有效url路径字符 必须以.jpeg,.jpg,.png或.gif结尾 我想这写起来很简单,但我不是一个很棒的正则表达式。 我想这些部件看起来像这样 ^((HTTPS \:\ / \ /)|(\ / {1,2})) (有任何想法吗?) ((JPE G |?PNG | GIF))$ 任何人都可以帮助我填补空白吗? 谢谢 回答 (https?:)?//?[^\'”]+?\.(jpg|jpeg|gif|png)

使用MS Word中的正则表达式替换文本 – C#

我有一个Word文档,我想打开并用“test”替换社会安全号码的所有实例。 我已经有了打开文档的代码。 这是替换的代码。 但是,我在这一点上使用正则表达式时遇到了麻烦: _wordApp.Selection.Find.Text =; 在我的代码中。 使用正则表达式是一种好方法还是有更好的方法? 请记住,我必须匹配任何社会安全号码…因此:\ b [0-9] {3} – [0-9] {2} – [0-9] {4} \ b或\ b [0-9] {3} [0-9] {2} [0-9] {4} \ b 提前致谢… object replaceAll = Werd.WdReplace.wdReplaceAll; _wordApp.Selection.Find.ClearFormatting(); _wordApp.Selection.Find.Text = ; _wordApp.Selection.Find.Replacement.ClearFormatting(); _wordApp.Selection.Find.Replacement.Text = “test”; _wordApp.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref […]

如何进行平衡组捕获?

假设我有这个文本输入。 tes{}tR{R{abc}aD{mnoR{xyz}}} 我想提取ff输出: R{abc} R{xyz} D{mnoR{xyz}} R{R{abc}aD{mnoR{xyz}}} 目前,我只能使用msdn中的平衡组方法提取{}组内的内容。 这是模式: ^[^{}]*(((?’Open'{)[^{}]*)+((?’Target-Open’})[^{}]*)+)*(?(Open)(?!))$ 有谁知道如何在输出中包含R {}和D {}?

使用Regex删除脚本标记

我正在尝试使用我在本网站上找到的正则表达式,它似乎不起作用。 有任何想法吗? 输入字符串 : sFetch = “123\n\t\tfunction utmx_section(){}function utmx(){}\n\t\t(function()})();\n\t456”; 正则表达式 : sFetch = Regex.Replace(sFetch, “.*?”, “”, RegexOptions.IgnoreCase);

正则表达式匹配任何单词 – 没有非贪婪的运算符

我希望将任何内容与特定单词匹配(例如,C */的结束注释),但是,由于性能原因,我不想使用非贪婪的运算符。 例如,要匹配C注释:/ /\*.*?\*/对我的文件来说太慢了。 有没有可能提高性能?