In this tutorial we will be learning:
-Regular Expressions
Requirements:
-A brain
-An ide (Visual basic 2008, 2010 what ever suits your needs)
-Keyboard
-Mouse
-Monitor
-Hand(s)
-Eyes
Regular Expressions -
Ok, i'm now going to teach you how to use regular expressions. We will be using RegExr for this tutorial located here: http://gskinner.com/RegExr/
Lets start by grabbing a simple integer:
I will now be explaining the code:
Dim match as matchcollection - This specifies match as part of the matchcollection class
Dim myregex as regex("expression") - This specifies myregex as an expression in the regular expressions class, i will be elaborating on the "expression" further on in the tutorial.
match = myregex.matches(ip) - This means "match" will have strings that are matched from the "myregex" expression.
For each successfulmatch in match - For every match that is found in the matchcollection class
Listbox1.items.add(successfulmatch) - It will add every match it found to the listbox
Next - What happens after the code has been executed
Now lets move onto the expression part of this tutorial, this will signify what its trying to find! Now, if there was a text with "udshfhsdf 127.0.0.1" And we wanted to grab only the ip address listed in there we will have to create an expression, for that go to the website listed above.
Step 1.
In the huge textbox paste in the string we are trying to parse:
Spoiler (Click to View)
Step 2:
Now once we have done that it's time to create our expression (Look on the right to the box where all the expressions are met)
Spoiler (Click to View)
Step 3:
When you click an expression you will see the action below.
Find the one that says: Matches any digit character: \d
Now we will put that in the textbox above the huge textbox where our string is. It should now look like this:
Spoiler (Click to View)
Step 4:
Now, it's grabbing every single digit in that expression, it does not grab the whole thing and its not even parsing the dots, to fix this find an expression that says: Matches any character that is not a whitespace character (spaces, tabs, line breaks).
Add that in front of the d, remember to put \expressionhere
Spoiler (Click to View)
Step 5:
Now its grabbing All the dots, and only a few of the digits. We now signaled it to grab the whole ip address, to do so add + in front of S.
We now have our expression : "\d\S+"
To take this into effect simply edit:
To
Easier way of grabbing the ip address:
Step 1:
We know that \d specifies a digit, an ip contains digits than a dot digits than a dot and finally the last set of digits. Simply change the expression to
\d+ To grab the first set of digits
Step 2:
Now we are manually going to tell it to parse the dot so in front of the + sign add \.
That will parse 127. I hope you can do the rest on your own ! If this tutorial helped you, you will know how to do it.
I hope you enjoyed my basic tutorial.
Credits:
Simran
-Regular Expressions
Requirements:
-A brain
-An ide (Visual basic 2008, 2010 what ever suits your needs)
-Keyboard
-Mouse
-Monitor
-Hand(s)
-Eyes
Regular Expressions -
Ok, i'm now going to teach you how to use regular expressions. We will be using RegExr for this tutorial located here: http://gskinner.com/RegExr/
Lets start by grabbing a simple integer:
Code
Dim match as matchcollection
Dim myregex as regex("expression")
Dim ip as string = "dfgfdgdfgfd 127.0.0.1"
match = myregex.matches(ip)
For each successfulmatch in match
Listbox1.items.add(successfulmatch)
NextI will now be explaining the code:
Dim match as matchcollection - This specifies match as part of the matchcollection class
Dim myregex as regex("expression") - This specifies myregex as an expression in the regular expressions class, i will be elaborating on the "expression" further on in the tutorial.
match = myregex.matches(ip) - This means "match" will have strings that are matched from the "myregex" expression.
For each successfulmatch in match - For every match that is found in the matchcollection class
Listbox1.items.add(successfulmatch) - It will add every match it found to the listbox
Next - What happens after the code has been executed
Now lets move onto the expression part of this tutorial, this will signify what its trying to find! Now, if there was a text with "udshfhsdf 127.0.0.1" And we wanted to grab only the ip address listed in there we will have to create an expression, for that go to the website listed above.
Step 1.
In the huge textbox paste in the string we are trying to parse:
Spoiler (Click to View)
Step 2:
Now once we have done that it's time to create our expression (Look on the right to the box where all the expressions are met)
Spoiler (Click to View)
Step 3:
When you click an expression you will see the action below.
Find the one that says: Matches any digit character: \d
Now we will put that in the textbox above the huge textbox where our string is. It should now look like this:
Spoiler (Click to View)
Step 4:
Now, it's grabbing every single digit in that expression, it does not grab the whole thing and its not even parsing the dots, to fix this find an expression that says: Matches any character that is not a whitespace character (spaces, tabs, line breaks).
Add that in front of the d, remember to put \expressionhere
Spoiler (Click to View)
Step 5:
Now its grabbing All the dots, and only a few of the digits. We now signaled it to grab the whole ip address, to do so add + in front of S.
We now have our expression : "\d\S+"
To take this into effect simply edit:
Code
Dim myregex as regex("expression")Code
Dim myregex as regex("\d\S+")Easier way of grabbing the ip address:
Step 1:
We know that \d specifies a digit, an ip contains digits than a dot digits than a dot and finally the last set of digits. Simply change the expression to
\d+ To grab the first set of digits
Step 2:
Now we are manually going to tell it to parse the dot so in front of the + sign add \.
That will parse 127. I hope you can do the rest on your own ! If this tutorial helped you, you will know how to do it.
I hope you enjoyed my basic tutorial.
Credits:
Simran


