In: Computer Science
write a regular expression that will, using capturing groups, find:
Replace the entire row with comma-delimited values in the following order:
import java.util.regex.*;
public class Main
{
public static void main(String[] args) {
String input = "Jane,V,Quinn,S4040,SO,B,[email protected],Q43-15-5883,318-377-4560,318-245-1144,Y";
String regexp = "([A-Za-z]+),+([A-Za-z]+),+([A-Za-z]+),+([0-9A-Za-z]+),+([A-Za-z]+),+([A-Z]),+([0-9A-Za-z@.]+),+([0-9A-Za-z-]+),+([0-9-]+),+([0-9-]+)";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(input);
matcher.find();
System.out.println("Firstname : " + matcher.group(1));
System.out.println("Middlename : " + matcher.group(2));
System.out.println("Lastname: " + matcher.group(3));
System.out.println("Program: " + matcher.group(4));
System.out.println("Rank: " + matcher.group(5));
System.out.println("Grade: " + matcher.group(6));
System.out.println("Email: " + matcher.group(7));
System.out.println("Student ID: " + matcher.group(8));
System.out.println("Home phone: " + matcher.group(9));
System.out.println("Work Phone: " + matcher.group(10));
}
}
Output:
Firstname : Jane
Middlename : V
Lastname: Quinn
Program: S4040
Rank: SO
Grade: B
Email: [email protected]
Student ID: Q43-15-5883
Home phone: 318-377-4560
Work Phone: 318-245-1144
I have done the program in Java.