![]() |
F.4 REGEX String Functions |
regexsearch() regexreplace$()regexsearch() searches for expressions within text and regexreplace$() searches and replaces 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 idxBelow 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
|
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |