You can use regular expressions to find all the words ending in "lex" in a given string using the following code in Swift:
main.swift390 chars11 lines
Here, we're using NSRegularExpression
to create a regular expression pattern that looks for one or more word characters (\\w*
) followed by "lex" and a word boundary (\\b
).
We then apply this pattern to the inputString
using the matches(in:range:)
method to get an array of NSTextCheckingResult
instances representing the matches.
We then extract the corresponding substrings from the inputString
using the Range(_:in:)
and String(_:)
initializers, and store them in an array words
. Finally, we print out the array words
.
gistlibby LogSnag