Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Regular expressions tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-29-2010, 01:38 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Regular expressions tutorial

Regular expressions are very powerful and make complicate parsing challenges much easier.
This short tutorial will describe the usage of regular expressions in Basic4android.
If you are not familiar with regular expressions you can find many good tutorials online. I recommend you to start with this one: Regular Expression Tutorial - Learn How to Use Regular Expressions

Basic4android uses Java regular expression engine. See this page for specific nuances related to this engine: Pattern (Java Platform SE 6)

Regular expressions methods in Basic4android start with the predefined object named Regex. You can write Regex followed by a dot to see the available methods.

All methods accept a pattern string. This is the regular expression pattern. Note that internally the compiled patterns are cached. So there is no performance loss when using the same patterns multiple times.

For each method there are two variants. The difference between the variants is that the second one receives an 'options' integer that affects the engine behavior. For now there are two option, CASE_INSENSITIVE and MULTILINE. CASE_INSENSITIVE makes the pattern matching be case insensitive. MULTILINE changes the string anchors ^ and & match the beginning and end of each line instead of the whole string.
Both options can be combined by calling Bit.Or(Regex.MULTILINE, Regex.CASE_INSENSITIVE).

Matching the whole string
IsMatch and IsMatch2 are good to validate user input. The result of these methods is true if the whole string matches the pattern.
For example the following code checks if a date string is formatted in a format similar to: 12-31-2010
Code:
    Log(Regex.IsMatch("\d\d-\d\d-\d\d\d\d""11-15-2010")) 'True
Log(Regex.IsMatch("\d\d-\d\d-\d\d\d\d""12\31\2010")) 'False
This pattern will also match the string "99-99-9999".

Splitting text
Split and Split2 splits a text around matches of the given pattern.
Simple case:
Code:
Dim data As String
data = 
"123,432,13,4,12,534"
Dim numbers() As String
numbers = 
Regex.Split(",", data)
Dim l As List
l.Initialize2(numbers)
Log(l)
Lists can be easily printed with Log so we add the array to the list.
The result is:



The comma followed by a single space is part of the list formatting. The expected values were parsed.

Now if the data value was "123, 432 , 13 , 4 , 12, 534"
The result wasn't perfect:



There are extra spaces which are part of the parsed values.

We can change the pattern to match a comma or white space:
Code:
numbers = Regex.Split("[,\s]", data)
The result is still not as we want it:



Many empty strings were added.
The correct pattern in this case is:
Code:
numbers = Regex.Split("[,\s]+", data)
Find matches in string
Here we have a long string and we want to find all matches of a pattern in the string. We can also use capture groups to get specific parts of the match.

As an example we will find and print email addresses in text:
Code:
Dim data As String
data = 
"Please contact mike@gmail.com or john@gmail.com"
Dim matcher1 As Matcher
matcher1 = 
Regex.Matcher("\w+@\w+\.\w+", data)
Do While matcher1.Find = True
    
Log(matcher1.Match)
Loop
This code prints:
mike@gmail.com
john@gmail.com

Note that this pattern is far from being a good pattern for email validation / matching.

In the second example we will use a Matcher with capturing groups to validate a date text. The pattern is similar to the pattern in the first example with the addition of parenthesis. These parenthesis mark the groups:
Code:
Log(IsValidDate("13-31-1212")) 'false
Log(IsValidDate("12-31-1212")) 'true

Sub IsValidDate(Date As StringAs Boolean
    
Dim matcher1 As Matcher
    matcher1 = 
Regex.Matcher("(\d\d)-(\d\d)-(\d\d\d\d)", Date)
    
If matcher1.Find = True Then
        
Dim days, months As Int
        months = matcher1.Group(
1'fetch the first captured group.
        days = matcher1.Group(2'fetch the second captured group
        If months > 12 Then Return False
        
If days > 31 Then Return False
        
Return True
    
Else
        
Return False
    
End If
End Sub
The groups feature is very useful. If you find yourself calling String.IndexOf together with String.Substring multiple times, it is a good hint that you should move to a Regex and Matcher.
Reply With Quote
  #2 (permalink)  
Old 12-29-2010, 03:29 PM
Knows the basics
 
Join Date: May 2007
Posts: 78
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Hi Erel,
Thanks for the insight... it sure does help inspired me to think harder..


Below is a quick StringParse sub that I did to retrieve a sample date




s = "12/31/2010"
s1 = StrParse(s,"/",2)
msgbox(s1,"Info") ' returns 2010


Sub StrParse(FirstStr As String, sSeparator As String, idx As Int) As String
Dim strArray() As String, l As List
strArray = Regex.Split("[" & sSeparator & "\s]", FirstStr)
l.Initialize2(strArray)
Return l.Get(idx)
End Sub



Rgds
WZSun
Reply With Quote
  #3 (permalink)  
Old 04-26-2011, 06:50 PM
Knows the basics
 
Join Date: Apr 2011
Posts: 66
Default RegexBuddy

Btw, this is a very good tool to speed up Regex issues.
RegexBuddy: Learn, Create, Understand, Test, Use and Save Regular Expression
Regards
Hans
Reply With Quote
  #4 (permalink)  
Old 09-27-2011, 12:12 PM
Foz's Avatar
Foz Foz is offline
Newbie
 
Join Date: Sep 2011
Posts: 8
Default

I think I'm missing something here...

If I do a Split, into a dynamic string array, how do I then get the resulting array size?
Reply With Quote
  #5 (permalink)  
Old 09-27-2011, 12:16 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Code:
Dim arr() As String
arr = 
Regex.Split(...)
For i = 0 To arr.Length - 1
 
Log(arr(i))
Next
Reply With Quote
  #6 (permalink)  
Old 09-27-2011, 12:22 PM
Foz's Avatar
Foz Foz is offline
Newbie
 
Join Date: Sep 2011
Posts: 8
Default


Thank you Erel!

sigh... I was doing an inline assign which you obviously can't do, and it didn't like it and thus never showed the Length field and wouldn't compile.

One of these days I'll get my head screwed on straight...
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expresion with RegExp or maybe an other lib? sitajony Questions (Windows Mobile) 2 05-01-2010 08:51 PM
Numeric Data Mask - Using Regular Expressions david Questions (Windows Mobile) 2 01-04-2008 01:13 PM
New Regular Expressions library is released Erel Announcements 1 09-10-2007 05:09 AM
Watch expressions agraham Bug Reports 5 08-15-2007 02:56 PM
Evaluating mathematical expressions Erel Code Samples & Tips 0 06-10-2007 06:56 AM


All times are GMT. The time now is 10:28 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0