Monday, November 25, 2013

Orchestrator: Search for Text in a textfile with Regluar Expressions

Well I had a case the other day where I need to search for a username in a textfile and add the username as affected user in Service Manager. So I started to work on my plan on how to make this dynamic and bulletproof.

Here is a snip of the textfile:












The line that I am looking for is "Affected User: u2121" and my goal is to get only the u2121 id everytime the activity is run in different text files.

So how can I do this? Well then I remembered Regular expressions. What did Technet say on that subject again?

Regular expressions let you match a string to a pattern. The regular expression can contain a number of different elements that define the pattern. 

Hmm... Pattern.. Interesting..  I know that all ID here start with a U and a chain of number like
 u2121,u3434,u1414,u3535 and so on... So regular expressions it is

Affected user: u2121 was the text in the textfile. If the ID would have the following pattern in every textfile searched we can use a regular expression.

 In the Search text I added u[1-9]+ and selected "Use regular expressions".













Then I did a test, and the matched text showed the ID in the textfile, Nice!.










So let's look at the text I searched for again. Well all ID started with a u then that would be my first search query. (Simple ;)

Then I know that the rest is numbers between 1-9. So I added [1-9] which means all between 1 and 9.

But the number of characters on the ID's was sometimes 4,5,6. so I added the + to the end to match all of them. Now it will find the ID either it is with 4,5,6 characters.

This was a quick intro in this and there are plenty of combinations to use. Check it out!

http://technet.microsoft.com/en-us/library/hh440535.aspx