
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print First Letter of Each Word Using Regex in Swift
In Swift, regex is known as a regular expression. It is used to create a pattern which helps in matching or extracting some specific port of the given string. We can create a regex instance with the help of a regular expression in regex literal or string. In this article, we are going to use regex to print the first letter of each word.
Example
Input: String = "Ram got first place" Output: "Rgfp "
Here, the output string contains the first letters of each word present in the given string.
In the following examples we are going to use the following methods ?
matches(in:options:range) method ?This method is used to get an array which contains all the matches of the regular expression.
Syntax
func matches(in:str, options:option, range:Range)
This function takes three arguments ?
str ? The input string.
option ? Represent the matching option to use.
Range ? A valid range of the string preform search.
enumerateMatches(in:options:range:using:) method ? This method allows a block to handle each regular expression match.
Syntax
func enumerateMatches(in:str, options:option, range:Range, using:code)
This function takes four arguments ?
str ? The input string.
option ? The matching options.
Tange ? The valid range for the string to test.
code ? It enumertes the matches of the regular expression.
Algorithm
Step 1 ? Create a function that takes an input string as a parameter.
Step 2 ? Now this function set up a regular expression.
Step 3 ? Then it performs pattern matching on the given string using the matches() function. This function takes two parameters string to be matched and the range within the string to perform matching.
Step 4 ? Extract the first letter of each character by iterating over the "matchRes" using the map() function. For each match, the corresponding substring is extracted from the given string and stores the first letter of each substring in the array.
Step 5 ? Print the final result after joining all the elements of the array using joined() function.
Step 6 ? Create a string.
Step 7 ? Call the function and pass the string as arguments in it.
Example 1
In the following Swift program, we will print the first letter of each word using regex. So we define a function which takes an input string and extracts the first letter of each word present in the string. This function has a variable which holds the regular expression "\b\w", where "\b" represents a word boundary which ensures that the match occurs at the start of each word and "\w" represents a character(A-Z, a-z, 0-9). Then it initializes the matching pattern using NSRegularExpression. Then it matches the string using the matches() function and extracts the first letter of each word using the map() function and prints the final result after joining all the first letters of words present in the string.
import Foundation import Glibc func displayFirstLetter(mStr: String) { let mpattern = "\\b\\w" let regex = try! NSRegularExpression(pattern: mpattern) let matchRes = regex.matches(in: mStr, range: NSRange(mStr.startIndex..., in: mStr)) let firstLetters = matchRes.map { match in String(mStr[Range(match.range, in: mStr)!]) } print("First letters are:", firstLetters.joined()) } let OriginalString = "Ronit play golf Very Well" print("Original String:", OriginalString) displayFirstLetter(mStr: OriginalString)
Output
Original String: Ronit play golf Very Well First letters are: RpgVW
Example 2
In the following Swift program, we will print the first letter of each word using regex. So we define a function which takes an input string and extracts the first letter of each word present in the string. This function has a variable which holds the regular expression "\b\w", where "\b" represents a word boundary which ensures that the match occurs at the start of each word and "\w" represents a character(A-Z, a-z, 0-9). Then it create an instance of NSRegularExpression. Then we uses enumerateMatches(in:options:range:using:) method to iterate over the match found by the regex in the input string. And finally display the first letter of all the word present in the given string.
import Foundation let inputStr = "Mona visits charminar" let regexPattern = "\\b\\w" if let regex = try? NSRegularExpression(pattern: regexPattern, options: []) { regex.enumerateMatches(in: inputStr, options: [], range: NSRange(inputStr.startIndex..., in: inputStr)) { (match, _, _) in if let range = match?.range(at: 0), let lRange = Range(range, in: inputStr) { let fLetter = inputStr[lRange] print("First Letters are:", fLetter) } } }
Output
First Letters are: M First Letters are: v First Letters are: c
Conclusion
So this is how we can print the first letter of each word using regex. Regex is the most powerful because it allows us to perform complex searches on multiple files very easily. They are useful for pattern matching, string replacement, extracting substrings, etc.