Matching Address using regular expressions
This Example describes the way to match address using regular expression.For this we are going to make program named Matching.java. The steps involved in program Matching.java are described below:-
String name = "\\p{Upper}":- Creating expression for name.Here {upper} means all the name should be in upper case.
String namePattern = "("+name +"){2,3}":-Causes the resulting regular expression to match from 2 to 3 repetitions of the preceding regular expression
String regex = "^"+ namePattern + "\\w+ .*, \\w+ "+ zipCode + "$":-Combining the above 2 string and making a single expression.
retrival = address.matches(regex):-By this we are attempting to match the entire input against the pattern.
Matching.java:-
public class Matching {
|
Output of the program:-
NO MATCH pattern: abc 888 rohini, NY 64332 regex is: ^(\p{Upper}){2,3}\w+ .*, \w+ \d{5}(-\d{4})?$ MATCH pattern: ABC 888 south ex, NY 64332-4453 regex is: ^(\p{Upper}){2,3}\w+ .*, \w+ \d{5}(-\d{4})?$ |