![]() |
|
[.NET] (TuT)Simple Regex(TuT) - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] (TuT)Simple Regex(TuT) (/showthread.php?tid=1602578) |
(TuT)Simple Regex(TuT) - Turmoil - Aug 14, 2011 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 matchcollectionI 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 * - IDontNeedAUsername - Aug 14, 2011 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. * - Salad - Aug 14, 2011 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. * - SIM-701256 - Aug 14, 2011 Cool tutorial helped me to understand regex :) Good tutorial :) * - IllBeBackSoon - Aug 14, 2011 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] * - Turmoil - Aug 14, 2011 (Aug 14, 2011 01:11 PM)Simran Wrote: So digit, dot, digit ,dot, digit, dot. I'll re-make my tutorial =] Forgot about adding chars manually * - IllBeBackSoon - Aug 14, 2011 (Aug 14, 2011 01:13 PM)Turmoil Wrote: So digit, dot, digit ,dot, digit, dot. Look at the picture I added. Should show you that mine is proper. * - Turmoil - Aug 14, 2011 (Aug 14, 2011 01:13 PM)Simran 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 =] |