mircscripting.info Forum Index mircscripting.info
#mIRCscripting Forum
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Wildcard and regular expression tutorial.

 
Post new topic   Reply to topic    mircscripting.info Forum Index -> Tutorials
View previous topic :: View next topic  
Author Message
tidy trax
earlybird


Joined: 15 Apr 2004
Posts: 8
Location: england

PostPosted: Thu Apr 15, 2004 1:25 pm    Post subject: Wildcard and regular expression tutorial. Reply with quote

Keywords.

String.

A string is just a single character or multiple characters.

Character.

A character is a single letter, number or other thing from the keyboard.

Wildcard.

A wildcard is a character that can be used to match multiple or single characters without having to specify the characters to match.

A basic understanding of remote events and user levels is assumed.

Explanation.

This tutorial is intended to give a basic understanding of wildcards (*, ?, &) and regular expressions ($regex, $regml, $regsub).

Wildcards and regular expression patterns will be blue, text that can be changed will be red and keywords will be green.

I'll start with a basic explanation of the wildcards.

&

& matches 1 or more characters seperated by a space.

on *:text:hello &:#:

This will match hello world, hello person or any other 2 word sentence starting with hello.

on *:text:& hello:#:

This will match any 2 word sentence ending with hello.

*

* matches 0 or more characters, and it doesn't matter what they are seperated by.

on *:text:hello*:#:

This will match helloish, hellobullo or any other word starting with hello, providing hello are the first 5 characters in the string.

on *:text:*hello:#:

This will match any word ending with hello, providing hello are the last 5 characters in the string.

?

? matches 0 or 1 characters and can be used anywhere in the string.

on *:text:h?llo:#:

This will match h followed by <any single character>
followed by llo.

So it will match hello, hallo, hullo, hillo, hollo, hbllo, etc.

Now onto $regex. Smile

$regex

The basic syntax of $regex is: $regex([name,]string,pattern), where [name,] is optional (used to reference the regex later using $regml(name,N)), string is the string you want to search, and pattern is the pattern you want to find in the string.

We'll start with one of the most basic regular expressions there is.

//echo -a $regex(hello world,/hello/)

This will be "1", because it found a match, hello is in hello world.

However if you were to use a capital h in hello:

//echo -a $regex(hello world,/Hello/)

It will be "0", because regular expressions are case-sensitive by default.

There are two ways to solve this "problem" (sometimes it's useful).

Number one:

//echo -a $regex(hello world,/(?i)Hello/)

The (?i) means that the following character will be matched case-insensitively.

Number two:

//echo -a $regex(hello world,/Hello/i)

The /i means the whole pattern is matched case-insensitively.

Now let's look at matching multiple patterns in a string.

//echo -a $regex(hello hello hello,/hello/)

Even though hello is in hello hello hello 3 times, it will only match once, because by default regular expressions will find a match and then stop searching.

Solution:

//echo -a $regex(hello hello hello,/hello/g)

This will be "3" because the /g tells it to return all matches.

| means OR.

//echo -a $regex(abc,/abd|abc/)

is "1" because it's set to match abc or abd and abc matched.

Grouping brackets are used to set what appears in $regml().

See the $regml area of this tutorial for more info.

Now we'll look at character grouping.

Character groups generally look like this: [characters here].

//echo -a $regex(hello,/[aeiou]/)

^ negates the group.

[^a-z] means any "non lower case letter", where as if you had left the ^ out, it would be any "lower case letter".

Instead of matching aeiou it will match a or e or i or o or u.

You can also use a-z for all lower case letters, A-Z for all upper case letters and 0-9 for all numbers.

There are also various \switches you can use.

\w is any word character, number or an underscore (_) ([a-zA-Z0-9_])

\W is any non-word character, number or an underscore (_) ([^a-zA-Z0-9_], opposite of \w)

\s is any whitespace character.

\S is any non-whitespace character.

\d is any digit ([0-9], 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9).

\D is any non-digit ([^0-9]).

Now we'll get onto repetition brackets ({}).

{1,} means "1 or more times".

{,1} means "1 or less times".

{1} means "1 time".

{1,3} means "1, 2, or 3 times".

Few examples:

\d{1} will match any digit.

\d{,3} will match 3 or less digits.

\d{3} will match 3 digits only.

\d{3,} will match any 3 digits or more.

Now we'll get onto wildcards used in regular expressions.

* means 0 or more characters ({0,}).

? means 0 or 1 characters ({0,1}).

+ means 1 or more characters ({1,}).

. means 1 character ({1}).

Few examples:

\d* means 0 or more digits.

\d+ means 1 or more digits.

\d. means a digit followed by any character.

\d? means 0 or 1 digits.

.* means 0 or more characters.

.+ means 1 or more characters.

.? means 0 or 1 characters

.. means 2 characters

This will do for now, if there's anything i missed that you want to know, feel free to ask in a reply, i will help if i can.

$regml

$regml is used to reference certain matches made in $regex, i will only give some basic examples on this.

//echo -a $regex(hello world,/(hello)/) $regml(1)

Will echo "1 hello" because it matched hello inside the grouping brackets.

//echo -a $regex(hello world hellor world,/(hell.*)/) $regml(1) $regml(2)

Will echo "1 hello world hellor world" because it matched hell.* twice inside the grouping brackets.

Now we'll go onto $regsub.

$regsub

$regsub is used to replace matches with other text/matches.

The syntax is $regsub(string,pattern,replace text,var), i will only give one example.

//var %x | //.echo -q $regsub(hello world,/world/,,%x) | //echo -s %x

This will match "world" in "hello world" and replace it with $null ($chr(0)).


If you have any questions on the tutorial please post a reply and i will try to help.
_________________
- tidy trax


Last edited by tidy trax on Fri Apr 16, 2004 8:57 pm; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
Parabola
Quite Active


Joined: 27 Dec 2003
Posts: 34
Location: Between Two Walls...

PostPosted: Fri Apr 16, 2004 3:55 pm    Post subject: Reply with quote

Great Tutorial !! Wink
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Mindless
Major


Joined: 27 Dec 2003
Posts: 114
Location: Antwerp, Belgium

PostPosted: Fri Apr 16, 2004 10:31 pm    Post subject: Reply with quote

good job tidy trax, keep it up!
_________________
"An eye for an eye makes the whole world blind."
-- Gandhi

*bump*
Back to top
View user's profile Send private message MSN Messenger
IL_Pizu
Major


Joined: 28 Dec 2003
Posts: 113
Location: .:Europe:. .:Malta:.

PostPosted: Wed Apr 21, 2004 8:50 am    Post subject: Reply with quote

gr8 tutorial Smile
_________________
Check This Out: http://www.shaddowdomain.com/scary.jpg Smile
http://pizu.modblog.com/ Smile
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
tidy trax
earlybird


Joined: 15 Apr 2004
Posts: 8
Location: england

PostPosted: Thu May 06, 2004 5:25 am    Post subject: update Reply with quote

Updated tutorial: click.
_________________
- tidy trax
Back to top
View user's profile Send private message MSN Messenger
moneo
Major


Joined: 27 Dec 2003
Posts: 58
Location: Savannah, GA, USA

PostPosted: Fri May 07, 2004 12:36 pm    Post subject: Reply with quote

Very good job, tidy. I especially like the colored format. Thanks!
_________________

me@lucasoman.com | http://lucasoman.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
hixxy
Quite Active


Joined: 19 Dec 2005
Posts: 25

PostPosted: Mon Jan 09, 2006 8:05 pm    Post subject: Reply with quote

That tutorial is also outdated. You can find the most up to date tutorial on mirc.net here or mircscripts.org here. mircscripts.org makes you wait a week to update projects so if I update the tutorial more than twice a week mirc.net would be the best place to check.
Back to top
View user's profile Send private message Visit poster's website
lallouSs
Quite Active


Joined: 09 Apr 2007
Posts: 15
Location: Lebanon - Beirut

PostPosted: Sun Apr 15, 2007 5:03 am    Post subject: Regex. Reply with quote

Well you can use http://www.mishscript.de/tutorials/regex1.htm and http://www.mishscript.de/tutorials/regex2.htm great regex tutorial.
_________________
Learn to be.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    mircscripting.info Forum Index -> Tutorials All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group