Regular expressions: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 2,213: | Line 2,213: | ||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
Basic example showing the use of SenseTalk's pattern language to create a pattern, test for a match, find all matches, and replace a match. |
|||
<lang sensetalk> |
<lang sensetalk> |
||
set text to "This is a story about R2D2 and C3P0 who are best friends." |
|||
set pattern to <word start, letter, digit, letter, digit, word end> |
|||
put the sixth word of text matches pattern -- (note: the sixth word is "R2D2") |
|||
put every occurrence of pattern in text |
|||
replace the second occurrence of pattern in text with "Luke" |
|||
put text |
|||
</lang> |
|||
Output |
|||
<lang sensetalk> |
|||
True |
|||
(R2D2,C3P0) |
|||
This is a story about R2D2 and Luke who are best friends. |
|||
</lang> |
|||
Advanced example showing how to use capture groups within a pattern to reformat the names in a list. |
|||
<lang sensetalk> |
|||
set phoneList to {{ |
|||
Harry Potter 98951212 |
|||
Hermione Granger 59867125 |
|||
Ron Weasley 56471832 |
|||
}} |
|||
set wordPattern to <word start, characters, word end> |
|||
set namePattern to <start of line, {firstName: wordPattern}, space, {lastName: wordPattern}> |
|||
replace every occurrence of namePattern in phoneList with "{:lastName}, {:firstName} –" |
|||
put phoneList |
|||
</lang> |
|||
Output |
|||
<lang sensetalk> |
|||
Potter, Harry – 98951212 |
|||
Granger, Hermione – 59867125 |
|||
Weasley, Ron – 56471832 |
|||
</lang> |
</lang> |
||