Regular expressions: Difference between revisions

→‎{{header|REXX}}: add REXX examples modeled after PERL examples. -- ~~~~
(Add NetRexx implementation)
(→‎{{header|REXX}}: add REXX examples modeled after PERL examples. -- ~~~~)
Line 1,285:
<br>through implementation specific extensions.
<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.
<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
 
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
/*stick a fork in it, we're done.*/</lang>
'''output'''
<pre style="overflow:scroll">
The string is: I am a string
It ends with: string
It does not start with: You
It contains the string: ring
It does not contain the word: ring
</pre>
<lang rexx>/*REXX program demonstrates substitution (modeled after Perl example).*/
$string = "I am a string"
old = " a "
new = " another "
say 'The original string is:' $string
say 'old word is:' old
say 'new word is:' new
$string = changestr(old,$string,new)
say 'The changed string is:' $string
/*stick a fork in it, we're done.*/</lang>
'''output'''
<pre style="overflow:scroll">
The original string is: I am a string
old word is: a
new word is: another
The changed string is: I am another string
</pre>
<lang rexx>/*REXX program shows non-destructive sub. (modeled after Perl example).*/
$string = "I am a string"
old = " a "
new = " another "
say 'The original string is:' $string
say 'old word is:' old
say 'new word is:' new
$string2 = changestr(old,$string,new)
say 'The original string is:' $string
say 'The changed string is:' $string2
/*stick a fork in it, we're done.*/</lang>
'''output'''
<pre style="overflow:scroll">
The original string is: I am a string
old word is: a
new word is: another
The original string is: I am a string
The changed string is: I am another string
</pre>
<lang rexx>/*REXX program shows test and substitute (modeled after Perl example).*/
$string = "I am a string"
old = " am "
new = " was "
say 'The original string is:' $string
say 'old word is:' old
say 'new word is:' new
 
if wordpos(old,$string)\==0 then
do
$string = changestr(old,$string,new)
say 'I was able to find and replace ' old " with " new
end
/*stick a fork in it, we're done.*/
</lang>
'''output'''
<pre style="overflow:scroll">
The original string is: I am a string
old word is: am
new word is: was
I was able to find and replace am with was
</pre>
 
=={{header|Ruby}}==