To identify particular pattern of regular expressions in Strings is achieved in JAVA by using the API java.util.regex. This API consists of two main java classes such as Matcher and Pattern classes. Also we can even use String Functions to identify the particular pattern of regular expressions in the String class.
Pattern Class
A regular expression specified as a String is passed into an instance of this class.
The output is used to create a Matcher object that can match the character sequences against the regular
expression.
Static methods available in the Pattern Class as follows:
- pattern()
- split()
- matcher()
- compile()
- matches()
Matcher Class
This java class helps to matches the character sequences by interpreting the pattern.
Static Matcher Methods in the API:
- start()
- end()
- group()
Identify the List of Strings Starts with a particular letter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * Print all the list of Strings starts with particular character * @author http://total-qa.com * */ public class Regex { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("guru99"); list.add("seleniumhq"); list.add("javatpoint"); list.add("total-qa"); String regex = "\\At"; Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); for(int i=0;i<list.size();i++) { Matcher matcher = pattern.matcher(list.get(i)); boolean result = matcher.find(); System.out.println("Result:: " + result ); while (result) { System.out.print("Start index: " + matcher.start()); System.out.print(" End index: " + matcher.end() + " "); System.out.println("Group value::"+matcher.group()); break; } } } } |
Output of the Program:
Result:: false
Result:: false
Result:: false
Result:: true
Start index: 0 End index: 1 Group value::t
String Functions to identify the Regular Expressions:
- startsWith(String prefix)
- endsWith(String suffix)
- contains(CharSequence s)
Identify the List of Strings Starts with a particular letter using String Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.util.ArrayList; import java.util.List; public class StringFunctions { public static void main(String[] args) { /* print all the names start with a particular character print all the names ends with a particular character print all the names contains as part of the String */ List<String> list = new ArrayList<String>(); list.add("total-qa"); list.add("javatpoint"); list.add("guru99"); list.add("tutorialpoint"); for(int i=0;i<list.size();i++) { String str = list.get(i); boolean result = str.startsWith("t"); if(result) System.out.println("String starts with a particular character:::" + str); } for(int i=0;i<list.size();i++) { String str = list.get(i); boolean result = str.endsWith("9"); if(result) System.out.println("String ends with a particular character:::" + str); } for(int i=0;i<list.size();i++) { String str = list.get(i); boolean result = str.contains("a"); if(result) System.out.println("String contains with a particular character:::" + str); } } } |
Output of the Program:
String starts with a particular character:::total-qa
String starts with a particular character:::tutorialpoint
String ends with a particular character:::guru99
String contains with a particular character:::total-qa
String contains with a particular character:::javatpoint
String contains with a particular character:::tutorialpoint
Conclusion:
Using the String functions and java.util.regex API, identification of the pattern in the given Strings is possible using the above examples.