Regex matching/text search
Kind of urgent.
Basically I want to find all matches of a particular sequence and extract the values for that and keep going/doing this search until no matches found.
I want to find text in between the tags:
<#some text#>
and get me all matches for me and with the ability to replace the text.
so if I had this string:
hi my name is <#name#> and I like <#userLikes#>
I want to get the tags in that text and replace the tags in the original text with whatever I want
Thanks
Re: Regex matching/text search
C# Code:
using System.Text.RegularExpressions;
C# Code:
// Match
Match m = Regex.Match("<#name#>", "\<\#([a-zA-Z]*)\#\>");
if(m.Success) //...
// Replace
string s = Regex.Replace("<#name#>", "\<\#([a-zA-Z]*)\#\>", "WHATEVER");
The Regular Expressions are wrong however you get the idea, that's how you do regular expression searching/matching/replacing in C#.
Re: Regex matching/text search
sure thanks. I am aware of that but need a regex pattern to do what I need it to do :) I'm not great with regex!
Re: Regex matching/text search
Ohhh sorry my bad... Errrm I'll have a play in RegexTutor and see what I can come up with then edit this post :)
EDIT: Well I guess that I was nearly there, try this one!
Re: Regex matching/text search
thanks! That didnt quite work :( the .Value gave me just a "<". My string is:
Please find the attachment in this email for <#new_candidate.new_name#>
Thanks!
so need to find the last tag/element (<# text here #>)
Re: Regex matching/text search
Weird, here's what RegexBuddy gave me for iterating over matches and match groups:
C# Code:
try {
Regex regexObj = new Regex("<#(.*?)#>");
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
for (int i = 1; i < matchResults.Groups.Count; i++) {
Group groupObj = matchResults.Groups[i];
if (groupObj.Success) {
// matched text: groupObj.Value
// match start: groupObj.Index
// match length: groupObj.Length
}
}
matchResults = matchResults.NextMatch();
}
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
(By the way, new RegEx in there - "<#(.*?)#>")
Re: Regex matching/text search
Thanks! That almost works. It works fine for this for example:
blah blah 'get field value' blah blah
but still not for:
blah blah <#get field value#> blah blah
im guessing because the < is a regex special character?
Re: Regex matching/text search
Okay try the regex "\<\#(.*?)\#\>". That should work. I read somewhere that you should escape all non-alphanumeric characters that are not being used in a regex match, now I know I should take note of these things. :)
Re: Regex matching/text search
That Regex seems to work okay for me. I get both matches.
c# Code:
string str = "hi my name is <#name#> and I like <#userLikes#>";
string exp = "<#(.*?)#>";
MatchCollection matches = Regex.Matches(str, exp);
foreach (Match m in matches)
{
MessageBox.Show(m.ToString());
}
Re: Regex matching/text search
Of course!
If you used "Match matchResults = regexObj.Match(subjectString);" it would only get the first match, wouldn't it?
Haven't used RegEx in C# much, but at least it works! :D
Re: Regex matching/text search
sorry I should have posted the code:
Code:
public string GetTemplateReplacedValues(string entityID, string originalTemplateText, string openingTag, string closingTag)
{
System.Collections.ArrayList entities = new System.Collections.ArrayList();
FetchData data = new FetchData();
BusinessLogicManager.CrmSdk.DynamicEntity entity = new BusinessLogicManager.CrmSdk.DynamicEntity();
string finalReplacedTemplate = String.Empty;
string regexPattern = openingTag + "(.*?)" + closingTag;
Regex patternToMatch = new Regex(regexPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
try
{
foreach(Match matchFound in patternToMatch.Matches(originalTemplateText))
{
string valueFound = matchFound.Value.Replace(openingTag, String.Empty);
valueFound = valueFound.Replace(closingTag, String.Empty);
valueFound = valueFound.Trim().ToLower();
string[] entityValue = valueFound.Split(new char[]{'.'});
if (entityValue.Length == 2)
{
entity = data.GetEntityFromCollection(entities, entityValue[0]);
if (entity.Name != entityValue[0])
{
entity = data.GetDynamicEntity(entityID, null, entityValue[0]);
if (entity != null)
{
if (entities.Contains(entity) == false)
{
entities.Add(entity);
}
}
}
string entityFieldValue = data.GetAttributeFieldValue(entity, entityValue[1], true);
finalReplacedTemplate = Regex.Replace(originalTemplateText, regexPattern, entityFieldValue, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
}
}
}
catch (Exception ex)
{
BusinessLogicManager.Settings.DoSendErrorEmail("Error whilst applying custom DR template: " + ex.ToString());
}
return finalReplacedTemplate;
}
Parameters I am passing are valued (opening and closing tags: "<#" "#>")
but still no matches for that sequence :(
Re: Regex matching/text search
Does it work if you have some simple code?
Namely try this in the middle of everything and comment out the rest:
C# Code:
try {
Regex regexObj = new Regex("<#(.*?)#>");
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
for (int i = 1; i < matchResults.Groups.Count; i++) {
Group groupObj = matchResults.Groups[i];
if (groupObj.Success) {
messageBox.Show(groupObj.Value);
}
}
matchResults = matchResults.NextMatch();
}
} catch (Exception ex) {
MessageBox.Show("It broke... ");
}
Re: Regex matching/text search
lol @ exception message. "It broke". LOL. :)
I will try your suggestion but still dont understand why it works except for <# #> tags. :-/
it works with:
'fieldvalue'
!!fieldvalue!!
but not <#fieldvalue#>
Re: Regex matching/text search
It seems to be the IgnorePatternWhitespace option and the "#". Try without that option.
Quote:
Eliminates unescaped white space from the pattern and enables comments marked with #.
Re: Regex matching/text search
Hmm well I did say to try the escaped regex, or is that counted too?
Re: Regex matching/text search
that simple code does work! :)
and so does mine now after I took out the IgnoreWhitespace option