Please i need some help with my programing assigment in Java Part 1. Use this te
ID: 3812620 • Letter: P
Question
Please i need some help with my programing assigment in Java
Part 1. Use this test harness to play with regular expressions. This is the same as the one in the tutorial, but it doesn't use the Console class. The Console was added to the jdk in version 1.6 import java ut i 1. Scanner; import import Java, uti regex Matcher public class Test public static void main (StringLI args) Scanner console new Scanner (System.in while true System. out.printin ("VnEnter your regex i String my Regex console next Line Pattern pattern Patter compile (myRegex) System. out print (ITV the String to search VnEnter String searchstring console next Line Matcher matcher attern match (searchString); er boolean found false while (una Lchepk Lind t tel out. for at. ("T fo the text V" s starting at "index Sd and ending a L index 8d. Sn" matcher group C), at. cher tart. matcher end found true if nd System out printin No match found. An Use the program above to demonstrate the use of Odot), star t (plus), Wd The use of t, and as greedy quantifiers.Explanation / Answer
The Java Regex or Regular Expression is an API to define pattern for searching or manipulating strings.
It is widely used to define constraint on strings such as password and email validation.
Java Regex API provides 1 interface and 3 classes in java.util.regex package.
java.util.regex package
It provides following classes and interface for regular expressions. The Matcher and Pattern classes are widely used in java regular expression.
Use of Symbols
A quantifier defines how often an element can occur. The symbols ?, *, + and {} define the quantity of the regular expressions
Regular Expression
.(Dot)
*(Star)
Any character (may or may not match terminator)
Occurs zero or more times, is short for {0,}
X* finds no or several letter X, <sbr /> .* finds any character sequence
+(Plus)
Occurs one or more times, is short for {1,}
X+- Finds one or several letter X
?
d
s
W
[]
()
Output of Program
Occurs no or one times, ? is short for {0,1}.
Any digits, short of [0-9]
Any whitespace character, short for [ ]
Any non-word character, short for [^w]
Range Operator, list of chars,
You can group parts of your regular expression. In your pattern you group elements with round brackets.
X? finds no or exactly one letter X
[a-z] matches any character in range a through z. [0-9] is a digit.
This allows you to assign a repetition operator to a complete group.
Regular Expression
Description Examples.(Dot)
*(Star)
Any character (may or may not match terminator)
Occurs zero or more times, is short for {0,}
X* finds no or several letter X, <sbr /> .* finds any character sequence
+(Plus)
Occurs one or more times, is short for {1,}
X+- Finds one or several letter X
?
d
s
W
[]
()
Output of Program
Enter your regex: java
Enter the String to Search: this is java, do you know java I found the text java starting at index 8 and ending at index 12 I found the text java starting at index 26 and ending at index 30
Occurs no or one times, ? is short for {0,1}.
Any digits, short of [0-9]
Any whitespace character, short for [ ]
Any non-word character, short for [^w]
Range Operator, list of chars,
You can group parts of your regular expression. In your pattern you group elements with round brackets.
X? finds no or exactly one letter X
[a-z] matches any character in range a through z. [0-9] is a digit.
This allows you to assign a repetition operator to a complete group.