Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

(TuT)Simple Regex(TuT)

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:

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

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:
Code
Dim myregex as regex("expression")
To
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
Thanks turmoil! I've been looking for a tutorial like this. The login request from youtube is fucking annoying with the GALX and the dsh.
I didn't not know about that site, but it seems great. Would have been useful just a few days earlier for me, but it's better to discover it later instead of never.
Cool tutorial helped me to understand regex :)
Good tutorial :)
Code
(\d+\.\d+\.\d+\.\d+)

Is the proper Regex to get the IP :)

As you can see here:

[img]http://simranuploader.com/dl/4e482c28e95c8-Capture.PNG[/img]
(Aug 14, 2011 01:11 PM)IllBeBackSoon Wrote:  
Code
(\d+\.\d+\.\d+\.\d+)

Is the proper Regex to get the IP :)

So digit, dot, digit ,dot, digit, dot.

I'll re-make my tutorial =] Forgot about adding chars manually
(Aug 14, 2011 01:13 PM)Turmoil Wrote:  So digit, dot, digit ,dot, digit, dot.

I'll re-make my tutorial =] Forgot about adding chars manually

Look at the picture I added. Should show you that mine is proper.
(Aug 14, 2011 01:13 PM)IllBeBackSoon Wrote:  Look at the picture I added. Should show you that mine is proper.

Yes, yours is proper.
I added an easier way referring to your method in the tutorial.
I just forgot about adding chars

Guys if you need help ask simran, hes a regex expert =]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by TheWarOwlAug 23, 2017 10:13 PM 2 121 Aug 23, 2017 10:13 PM
Last Post: TheWarOwl
[.NET] by WhatIsRSOct 31, 2016 07:51 PM 7 245 Oct 31, 2016 07:51 PM
Last Post: Ninja
[.NET] by shooterbretJun 2, 2015 10:00 AM 3 110 Jun 2, 2015 10:00 AM
Last Post: LUOIdeRentA