Regular expressions: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
(Added Wren) |
||
Line 2,768: | Line 2,768: | ||
That's it. The resulting binary will print |
That's it. The resulting binary will print |
||
'String "string$" matches string "This is a string"'</lang> |
'String "string$" matches string "This is a string"'</lang> |
||
=={{header|Wren}}== |
|||
{{libheader|Wren-pattern}} |
|||
Wren doesn't have regular expressions as such but the above module allows us to do simple string pattern matching and replacement in a similar fashion. SenseTalk's examples have been borrowed for this purpose. |
|||
<lang ecmascript>import "/pattern" for Pattern |
|||
var s = "This is a story about R2D2 and C3P0 who are best friends." |
|||
var p = Pattern.new("/u/d/u/d") |
|||
var matches = p.findAll(s) |
|||
System.print("Original string:\n%(" %(s)")") |
|||
System.print("\nThe following matches were found:") |
|||
matches.each{ |m| System.print(" %(m.text) at index %(m.index)") } |
|||
System.print("\nAfter replacing the second match:") |
|||
System.print(" %(p.replace(s, "Luke", 2, 1))") // replace 2nd match with "Luke" |
|||
System.print("\nReformatted phone list example:") |
|||
var phoneList = [ |
|||
"Harry Potter 98951212", |
|||
"Hermione Granger 59867125", |
|||
"Ron Weasley 56471832" |
|||
] |
|||
var p2 = Pattern.new("[+1/a] [+1/a] [=8/d]") |
|||
for (record in phoneList) { |
|||
var m = p2.find(record) |
|||
var t = m.capsText |
|||
System.print(" %(t[1]), %(t[0]) - %(t[2])") |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Original string: |
|||
This is a story about R2D2 and C3P0 who are best friends. |
|||
The following matches were found: |
|||
R2D2 at index 22 |
|||
C3P0 at index 31 |
|||
After replacing the second match: |
|||
This is a story about R2D2 and Luke who are best friends. |
|||
Reformatted phone list example: |
|||
Potter, Harry - 98951212 |
|||
Granger, Hermione - 59867125 |
|||
Weasley, Ron - 56471832 |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |