|
-
Aug 2nd, 2007, 07:23 AM
#1
Thread Starter
PowerPoster
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
Last edited by Techno; Aug 2nd, 2007 at 07:50 AM.
-
Aug 2nd, 2007, 08:32 AM
#2
Hyperactive Member
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#.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Aug 2nd, 2007, 08:40 AM
#3
Thread Starter
PowerPoster
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!
-
Aug 2nd, 2007, 08:43 AM
#4
Hyperactive Member
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!
Last edited by RudiVisser; Aug 2nd, 2007 at 08:47 AM.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Aug 2nd, 2007, 09:32 AM
#5
Thread Starter
PowerPoster
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 #>)
-
Aug 2nd, 2007, 09:45 AM
#6
Hyperactive Member
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 - "<#(.*?)#>")
Last edited by RudiVisser; Aug 2nd, 2007 at 09:50 AM.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Aug 2nd, 2007, 10:34 AM
#7
Thread Starter
PowerPoster
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?
-
Aug 2nd, 2007, 10:45 AM
#8
Hyperactive Member
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.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Aug 2nd, 2007, 10:48 AM
#9
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());
}
-
Aug 2nd, 2007, 11:00 AM
#10
Hyperactive Member
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!
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Aug 2nd, 2007, 11:04 AM
#11
Thread Starter
PowerPoster
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
Last edited by Techno; Aug 2nd, 2007 at 11:12 AM.
-
Aug 2nd, 2007, 11:17 AM
#12
Hyperactive Member
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... ");
}
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Aug 2nd, 2007, 11:26 AM
#13
Thread Starter
PowerPoster
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#>
-
Aug 2nd, 2007, 11:27 AM
#14
Re: Regex matching/text search
It seems to be the IgnorePatternWhitespace option and the "#". Try without that option.
Eliminates unescaped white space from the pattern and enables comments marked with #.
-
Aug 2nd, 2007, 12:13 PM
#15
Hyperactive Member
Re: Regex matching/text search
Hmm well I did say to try the escaped regex, or is that counted too?
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Aug 3rd, 2007, 04:31 AM
#16
Thread Starter
PowerPoster
Re: Regex matching/text search
that simple code does work!
and so does mine now after I took out the IgnoreWhitespace option
Last edited by Techno; Aug 3rd, 2007 at 04:39 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|