Results 1 to 16 of 16

Thread: Regex matching/text search

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Regex matching/text search

    C# Code:
    1. using System.Text.RegularExpressions;

    C# Code:
    1. // Match
    2. Match m = Regex.Match("<#name#>", "\<\#([a-zA-Z]*)\#\>");
    3. if(m.Success) //...
    4. // Replace
    5. 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.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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!

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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!
    Code:
    <#([a-zA-Z]*?)#>
    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.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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 #>)

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Regex matching/text search

    Weird, here's what RegexBuddy gave me for iterating over matches and match groups:
    C# Code:
    1. try {
    2.     Regex regexObj = new Regex("<#(.*?)#>");
    3.     Match matchResults = regexObj.Match(subjectString);
    4.     while (matchResults.Success) {
    5.         for (int i = 1; i < matchResults.Groups.Count; i++) {
    6.             Group groupObj = matchResults.Groups[i];
    7.             if (groupObj.Success) {
    8.                 // matched text: groupObj.Value
    9.                 // match start: groupObj.Index
    10.                 // match length: groupObj.Length
    11.             }
    12.         }
    13.         matchResults = matchResults.NextMatch();
    14.     }
    15. } catch (ArgumentException ex) {
    16.     // Syntax error in the regular expression
    17. }

    (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.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  9. #9
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Regex matching/text search

    That Regex seems to work okay for me. I get both matches.

    c# Code:
    1. string str = "hi my name is <#name#> and I like <#userLikes#>";
    2. string exp = "<#(.*?)#>";
    3. MatchCollection matches = Regex.Matches(str, exp);
    4.  
    5. foreach (Match m in matches)
    6. {
    7.     MessageBox.Show(m.ToString());                
    8. }

  10. #10
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  12. #12
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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:
    1. try {
    2.     Regex regexObj = new Regex("<#(.*?)#>");
    3.     Match matchResults = regexObj.Match(subjectString);
    4.     while (matchResults.Success) {
    5.         for (int i = 1; i < matchResults.Groups.Count; i++) {
    6.             Group groupObj = matchResults.Groups[i];
    7.             if (groupObj.Success) {
    8.                 messageBox.Show(groupObj.Value);
    9.             }
    10.         }
    11.         matchResults = matchResults.NextMatch();
    12.     }
    13. } catch (Exception ex) {
    14.     MessageBox.Show("It broke... ");
    15. }
    » 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.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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#>

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  14. #14
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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 #.

  15. #15
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width