Regular expressions: Difference between revisions

Content added Content deleted
(solved for maxscript)
m (→‎{{header|REXX}}: implemented version names, added whitespace and changed indentations.)
Line 1,610: Line 1,610:


=={{header|REXX}}==
=={{header|REXX}}==

Rexx does not directly support the use of regular expressions as part of the language.
Rexx does not directly support the use of regular expressions as part of the language.
<br>However, some rexx interpreters offer support for regular expressions via external function libraries or
<br>However, some rexx interpreters offer support for regular expressions via external function libraries or
Line 1,616: Line 1,615:
<br><br>It is also possible to emulate regular expressions through appropriate coding techniques.
<br><br>It is also possible to emulate regular expressions through appropriate coding techniques.
<br><br>All of the following REXX examples are modeled after the '''PERL''' examples.
<br><br>All of the following REXX examples are modeled after the '''PERL''' examples.
<br>'''testing'''
<lang rexx>/*REXX program demonstrates testing (modeled after Perl example).*/
$string = "I am a string"
say 'The string is:' $string
x = "string"
if right($string,length(x))=x then say 'It ends with:' x


===testing===
y = "You"
<lang rexx>/*REXX program demonstrates testing (modeled after Perl example).*/
if left($string,length(y))\=y then say 'It does not start with:' y
$string="I am a string"

say 'The string is:' $string
z = "ring"
if pos(z,$string)\==0 then say 'It contains the string:' z
x="string" ; if right($string,length(x))=x then say 'It ends with:' x
y="You" ; if left($string,length(y))\=y then say 'It does not start with:' y

z="ring" ; if pos(z,$string)\==0 then say 'It contains the string:' z
z = "ring"
if wordpos(z,$string)==0 then say 'It does not contain the word:' z
z="ring" ; if wordpos(z,$string)==0 then say 'It does not contain the word:' z
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</lang>
{{out}}
{{out}}
Line 1,640: Line 1,633:
It does not contain the word: ring
It does not contain the word: ring
</pre>
</pre>

'''substitution &nbsp; (destructive)'''
===substitution &nbsp; (destructive)===
<lang rexx>/*REXX program demonstrates substitution (modeled after Perl example).*/
<lang rexx>/*REXX program demonstrates substitution (modeled after Perl example).*/
$string = "I am a string"
$string = "I am a string"
old = " a "
old = " a "
new = " another "
new = " another "
say 'The original string is:' $string
say 'The original string is:' $string
say 'old word is:' old
say 'old word is:' old
say 'new word is:' new
say 'new word is:' new
$string = changestr(old,$string,new)
$string = changestr(old,$string,new)
say 'The changed string is:' $string
say 'The changed string is:' $string
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</lang>
{{out}}
{{out}}
<pre>
<pre style="overflow:scroll">
The original string is: I am a string
The original string is: I am a string
old word is: a
old word is: a
Line 1,658: Line 1,652:
The changed string is: I am another string
The changed string is: I am another string
</pre>
</pre>

'''substitution &nbsp; (non-destructive)'''

===substitution &nbsp; (non-destructive)===
<lang rexx>/*REXX program shows non-destructive sub. (modeled after Perl example).*/
<lang rexx>/*REXX program shows non-destructive sub. (modeled after Perl example).*/
$string = "I am a string"
$string = "I am a string"
Line 1,664: Line 1,660:
new = " another "
new = " another "
say 'The original string is:' $string
say 'The original string is:' $string
say 'old word is:' old
say 'old word is:' old
say 'new word is:' new
say 'new word is:' new
$string2 = changestr(old,$string,new)
$string2 = changestr(old,$string,new)
say 'The original string is:' $string
say 'The original string is:' $string
Line 1,678: Line 1,674:
The changed string is: I am another string
The changed string is: I am another string
</pre>
</pre>

'''test and substitute'''
===test and substitute===
<lang rexx>/*REXX program shows test and substitute (modeled after Perl example).*/
<lang rexx>/*REXX program shows test and substitute (modeled after Perl example).*/
$string = "I am a string"
$string = "I am a string"
Line 1,684: Line 1,681:
new = " was "
new = " was "
say 'The original string is:' $string
say 'The original string is:' $string
say 'old word is:' old
say 'old word is:' old
say 'new word is:' new
say 'new word is:' new


if wordpos(old,$string)\==0 then
if wordpos(old,$string)\==0 then
do
do
$string = changestr(old,$string,new)
$string = changestr(old,$string,new)
Line 1,700: Line 1,697:
I was able to find and replace am with was
I was able to find and replace am with was
</pre>
</pre>

Some older REXXes don't have a '''changestr''' bif, so one is included here [[CHANGESTR.REX]].
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, so one is included here: &nbsp; [[CHANGESTR.REX]].
<br><br>
<br><br>