.NET Framework Bookmark and Share   
 index > Regular Expressions > having trouble capturing {{something}}
 

having trouble capturing {{something}}

I tried the following Regex strings with ignorecase+explicitCapture without luck:
(?<pp>{{[a-z]([a-z0-0-]*)}})
(?<pp>\{\{[a-z]([a-z0-0-]*)\}\})

what else can I try to capture values like "{{something}}"

fs - new to w7
Try:

\{{2}(?<Word>[^\}]*)\}{2}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove
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 word = "testing {{something}} and {{next}}";
            Regex regex = new Regex(@"\{{2}(?<Word>[^\}]*)\}{2}", RegexOptions.IgnoreCase);
            Match m = regex.Match(word);
            while (m.Success)
            {
                Console.WriteLine(m.Groups["Word"].Value);
                Console.WriteLine("");
                m = m.NextMatch();
            }
            Console.ReadLine();
        }
    }
}

John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Proposed As Answer byinetscan Thursday, September 03, 2009 9:47 PM
  •  
JohnGrove
Thank you very much, John.

I remembered something from Unix about [] expression and fouNd out before reading your reply:
(?<pp>[{][{]\w([a-z0-0-]*)[}][}])
worked just perfectly in my regex utility against some test data


"regexDataGridView.CurrentRow.Selected = bCurRowSelectd; (?<preparse>{{yyyy-mm-dd}}regexDataGridView.CurrentRow.Selected = bCurRowSelectd;"
==>>{{yyyy-mm-dd}}

I guess I was not clear in the OP that I was actually looking for the "word" as well as surrounding delmiiter

BTW using your idea (?<Word>\{{2,2}([^\}]*)\}{2,2}) with the explict captureGroup name being ${Word} works like charm. Thank you.

For a little while Iwonder why I was getting {{word}} instead of the desired result until I saw the uppercase and lowercase typo I made

my regex utility uses the following for explicit capture after the neccesary code for compiling matching....
.....//the code was optimized to allow future interface to call and use various methods
     // instead of complete isolation
     // external interface expected to allow another project to use

internal bool findMatchExplicit(bool bSingle, string [] MatchGrpValuWantd,string strMatchGrpVarName,
	ref string strReslt)
{

	int i = 0;
	bool bSuccess = false;

	if (myMatch.Count <= 0) { setStatus("No match Found"); return bSuccess; }
	StringBuilder mybuf = new StringBuilder("");

	foreach (Match m in myMatch)
	{
		//int ig = m.Groups.Count;
		//string sg = m.Groups["DnlodDt"].Value.ToString();
		//string sg2 = m.Groups[1].Value.ToString();

		//int iro = Int32.Parse(textBoxRegexOption.Text);
		//wrong strReslt = regexReplace(m.ToString(), regexTextBox.Text, strMatchGrpVarName, (RegexOptions)iro); // regexOptionListBox.SelectedIndex);
		//got empty string: strReslt = regexReplace(m.ToString(), sInputPrePased, strMatchGrpVarName, (RegexOptions)iro); // regexOptionListBox.SelectedIndex);
		strReslt = "";
		foreach ( string s in MatchGrpValuWantd)  strReslt += "\t" + m.Result(s);	//	strMatchGrpVarName);
		
		if (removeBlankLineCheckBox.Checked)
		{
			//TODO //********************* need work */
			if (strReslt.IndexOf(" \r\n") >= 0) try
				{
					strReslt = Regex.Replace(strReslt, "( +\r\n)+", "\r\n").Trim();
				}
				catch (Exception ex) { addMsg("Exception error in removing empty lines, ignored "+ ex.Message); }
			if (strReslt.IndexOf(CRLF) >= 0) strReslt = strReslt.Replace(CRLF + CRLF, "").Trim();

		//TODO //********************* need work */
		}
		i++;
		if (i > 0) { mybuf.Append(CScrlf).Append(strReslt); }
		else
		{
			mybuf.Append(strReslt);
			if (bSingle) break;
		}
	}

	strReslt = mybuf.ToString().Substring(3);	// remove leading tab and CRLF
	setStatus("record found count=" + i);

	if (i > 0)
	{
		bSuccess = true;
		textBoxData.Text = strReslt;
	}
	
	return bSuccess;
}   // end findMatchExplicit






I have split compiling and match in the utility to allow trying different Regex string quickly from the utility's gui interface.

I am sure someone can come up with a better pattern and more efficient design. However, this utility serves the purpose of allowing to try, test patterns against test data before I implment in the main application I am trying to build.

I am aware that that are many pre-built regx utilityout there, But mine lets me store test data, as well as pattern, regex options, capture groups.
fs - new to w7

You can use google to search for other answers

Custom Search

More Threads

• How to make bbcode parser using Regex
• Web Table Extraction
• Help with regex.split
• Very basic question
• Need a Regular Expression that replaces drive letters with thier admin share equivalent
• Plz help me understand RegexOptions.Compiled option
• how to catch the email address string
• Include underscore in password verification
• split string based in some characters (mathematical operators)
• GroupMatch matches the ending slash when it shouldn't.