Can someone please help me with RegEx?

J

Julia

assuming I have template HTML file which contains property names to evaluate
at run time

<HTML>
<BODY><%PropertyName1%>
<DIV ID="<%PropertyName2%>">
</HTML>


How do I find all matches for <%...%> ,i dont know which pattern to use.

string pattern="";

string input=".....";



Regex rex = new Regex(pattern, RegexOptions.IgnoreCase);

for (Match match = rex.Match(input);match.Success;match = match.NextMatch())

{

Console.WriteLine("Found '{0}' at position {1}",match.Value, match.Index);

}


Thanks in advance
 
M

Michael Voss

Julia wrote:

[...snip...]
string pattern="";

// Maybe this is what you are looking for...
string pattern = said:
string input=".....";



Regex rex = new Regex(pattern, RegexOptions.IgnoreCase);

for (Match match = rex.Match(input);match.Success;match = match.NextMatch())

{

Console.WriteLine("Found '{0}' at position {1}",match.Value, match.Index);

}
[...snip...]
 
J

James Curran

First of all, life becomes much easier, if you use a one-character token
to introduce the property name, so how 'bout we change this to:

<HTML>
<BODY>{PropertyName1}
<DIV ID="{PropertyName2}">
</HTML>

(You weren't planning on using braces {} in your HTML for anything else,
right?


So then you want to find an opening brace {, followed by a number of
characters * which can be anything except a closing brace [^}], immediately
followed by a close brace:
{[^}]*}

Next, we want to capture the name of the property, so we wrap that part of
parens:
{([^}]*)}

Finally, { & } mean something special to the regex parser, so we have to
escape them:

\{([^\}]*)\}



--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
J

Julia

Thanks.it is working great.

James Curran said:
First of all, life becomes much easier, if you use a one-character token
to introduce the property name, so how 'bout we change this to:

<HTML>
<BODY>{PropertyName1}
<DIV ID="{PropertyName2}">
</HTML>

(You weren't planning on using braces {} in your HTML for anything else,
right?


So then you want to find an opening brace {, followed by a number of
characters * which can be anything except a closing brace [^}], immediately
followed by a close brace:
{[^}]*}

Next, we want to capture the name of the property, so we wrap that part of
parens:
{([^}]*)}

Finally, { & } mean something special to the regex parser, so we have to
escape them:

\{([^\}]*)\}



--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Julia said:
assuming I have template HTML file which contains property names to evaluate
at run time

<HTML>
<BODY><%PropertyName1%>
<DIV ID="<%PropertyName2%>">
</HTML>


How do I find all matches for <%...%> ,i dont know which pattern to use.

string pattern="";

string input=".....";



Regex rex = new Regex(pattern, RegexOptions.IgnoreCase);

for (Match match = rex.Match(input);match.Success;match = match.NextMatch())

{

Console.WriteLine("Found '{0}' at position {1}",match.Value, match.Index);

}


Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top