Sheerpower Logo
F.4  REGEX String Functions
Regular expressions, also known as REGEX, were first developed in the 1950s and have been enhanced and used ever since for text manipulation and searching. Sheerpower includes two functions that allow the use of REGEX expressions:
regexsearch() regexreplace$()
regexsearch() searches for expressions within text and regexreplace$() searches and replaces text.

Here is an example of using the regexsearch() function to search text for the word "rain". When found, the function returns the location of the word and stores it into the variable starting and stores the length of the word into _integer. Printing mid(text$,starting,_integer) show us the word rain within the text.
text$ = 'it is raining outside' expr$ = 'rain' starting = regexsearch(text$, expr$) print mid(text$, starting, _integer)
In this next example we search the text for the pattern of a phone number. There are three sets of () in the expression. Each of these is called an "expression group". In this example the first group is the area code. The second and third groups are additional parts of a phone number.
text$ = '505-555-1212' expr$ = '(\d{3})-(\d{3})-(\d{4})' for idx = 0 to 999 starting = regexsearch(text$, expr$,idx) if starting = 0 then exit for print idx, mid(text$, starting, _integer) next idx
Below is a simple replacement. The expression "rain" is found in the text and changed to the word "snow".
text$ = 'it is raining outside' expr$ = 'rain' print regexreplace$(text$, expr$, 'snow')
In this next example, we search the text for their age. When found, we display it and then change it to 65.
text$ = 'I am 39 years old' expr$ = '(\d+)' starting = regexsearch(text$, expr$, 1) print 'The age is '; mid(text$, starting, _integer) text$[starting:starting+_integer-1] = '65' // change the age to 65 print text$
For detailed information on regular expressions, read about Regular Expressions or watch this YouTube tutorial on Regular Expressions.
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen