Regular expressions: Difference between revisions

Added PicoLisp
No edit summary
(Added PicoLisp)
Line 500:
<lang php>$string = preg_replace('/\ba\b/', 'another', $string);
echo "Found 'a' and replace it with 'another', resulting in this string: $string\n";</lang>
 
=={{header|PicoLisp}}==
===Calling the C library===
PicoLisp doesn't have built-in regex functionality.
It is easy to call the native C library.
<lang PicoLisp>(let (Pat "a[0-9]z" String "a7z")
(use Preg
(native "@" "regcomp" 'I '(Preg (64 B . 64)) Pat 1) # Compile regex
(when (=0 (native "@" "regexec" 'I (cons NIL (64) Preg) String 0 0 0))
(prinl "String \"" String "\" matches regex \"" Pat "\"") ) ) )</lang>
Output:
<pre>String "a7z" matches pattern "a[0-9]z"</pre>
===Using Pattern Matching===
Regular expressions are static and inflexible. Another possibility is
dynamic pattern matching, where arbitrary conditions can be programmed.
<lang PicoLisp>(let String "The number <7> is incremented"
(use (@A @N @Z)
(and
(match '(@A "<" @N ">" @Z) (chop String))
(format (pack @N))
(prinl @A "<" (inc @) ">" @Z) ) ) )</lang>
Output:
<pre>The number <8> is incremented</pre>
 
=={{header|PowerShell}}==
Anonymous user