[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regular Expression Needed

Posted on 2004-11-08
9
Medium Priority
?
250 Views
Last Modified: 2010-04-15
I would like a way to find all unique string that fit the following pattern:  
-----------------------------
[!output SomeText]
(the  SomeText part is, of course, any text string)
-----------------------------

For example i have a string like this:
-----------------------------
Hello [!output FullName],

Welcome to [!output Destination].  Please email us with any questions at [!output ContactEmail].  We hope you enjoy your stay at [!output Destination].

Sincerly,

[!output Signature]
-----------------------------
 I would like to get a list back of the distinct [!output ] tags.  For example, in this letter it would return a list something like this:

{[!output FullName], [!output Destination], [!output ContactEmail], [!output Signature]}

Or something like that, that will be easy to split after the values are returned.

Thanks for your help.
0
Comment
Question by:Caliguian
  • 6
  • 3
9 Comments
 

Author Comment

by:Caliguian
ID: 12523594
The following is what I came up with, but I am looking for a better way of doing it and I think that someone that is good with regular expressions might be able to come up with a better way:

protected SortableList GetReplacementNamesList(string inputString)
{
SortableList tags = new SortableList();
int startIndex = 0, endIndex = 0;
while((startIndex = inputString.IndexOf("[!output")) > -1)
{
      endIndex = inputString.IndexOf("]", startIndex)+1;
      string tag = inputString.Substring(startIndex,endIndex-startIndex);
      if(!tags.Contains(tag)) tags.Add(tag);
      inputString = inputString.Substring(endIndex);
}
return tags;
}
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12524147
Try this...

using System.Text.RegularExpressions;

string myString = "Hello [!output FullName],\r\n\r\n";
myString += "Welcome to [!output Destination].  Please email us with any questions at [!output ContactEmail].  We hope you enjoy your stay at [!output Destination].\r\n\r\n";
myString += "Sincerly, ";
Regex regex = new Regex(
    @"(\[\!output\s\w+])",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

MatchCollection matches= regex.Matches(myString);

// Now all matches are in the matches collection...
// access like:
for ( int i = 0; i < matches.Count; i++ ) {
   matches[i];
}

HTH
0
 

Author Comment

by:Caliguian
ID: 12525259
that looks good, but it doesn't seem to find all the matches.  For example, i passed in this string:

"Your action plan response was due <b><font color=\"red\">[!output AuditResult.FollowUpDueDate][!output AuditResult.FollowUpDueDate]</font></b> and is now <b><font color=\"red\">overdue</font></b> Follow the instructions below on how to provide your action plan response.<p />Response Format:<br><li>Access the action plan report by going <a href=\"http://madrid/WebApp/PlugIns/GeneralMills/SfActionPlan.aspx?id=[!output AuditResult.AuditResultGlobalID]&passcode=[!output FollowUpPasscode]\">here.</a></li><li>Use the unique passcode provided.</li></br>"

and it only reported one match, the [!output FollowUpPasscode].

Is there something missing?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 1000 total points
ID: 12525323
That is due to the . the \w sais that it has to be alphanumeric...

Try this regex....

Regex regex = new Regex(
    @"(\[!output\s[A-Za-z0-9.]*\])",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

then if there are other special characters that you need to account for add them after the dot... depending on what they are you may have to escape them for instance if you want to look for a backslash "\" you would have to put in two backslashes (\\).
0
 

Author Comment

by:Caliguian
ID: 12525476
Great.  Thanks.  Actually the "." needed the backslash as well, but it works great.  thanks.
0
 

Author Comment

by:Caliguian
ID: 12525538
One more thing if your up for it; once i have the collection of matches, i would like to also get the value in between the [!output ] tags.  I still need the complete string (as your example above provided) but i would like to also be able to strip the tags off of a single match.  Any quick ideas on how to do that?
0
 

Author Comment

by:Caliguian
ID: 12525583
oh, this is not returning just the distincts, it is returning every instance.  any way to get just the distincts?
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12534701
To get the value is easy... just put another set of parens around the portions that you want...

Regex regex = new Regex(
    @"(\[!output\s([A-Za-z0-9\.]*)\])",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

MatchCollection matches = regex.Matches( myString );

Then I believe that you will access the matches to get the value you want like:
string aString = matches[0].Groups[2].Value;

As far as getting a unique list of the top of my head I would move what you want into another collection...

ArrayList alValues = new ArrayList();
foreach ( Match match in matches ) {
   if ( !alValues.Contains( match.Groups[2].Value ) ) {
      alValues.Add( match.Groups[2].Value);
   }
}

When you are done alValues will contain a uniques list of just the values inside the output tag... This may be able to be done with a custom IComparer on the collection also...

HTH
0
 

Author Comment

by:Caliguian
ID: 12535194
Thanks for the second regular expression.  I did end up moving the results to another collection to get the distincts.  Thanks for all your help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question