Regular expression segmentation in Java! Two simple cases are enclosed

Split function:
public String[] split(String regex) of String class
Splits the string based on the match of the given regular expression
For example: QQ search for friends
Search friends

  • Gender: Female
  • Range: "18-24" means age > = 18 & & age < = 24

The code is shown as follows:

public class RegexDemo {
	public static void main(String[] args) {
		//Define an age search range
		String ages = "18-24";
		
		//Definition rules
		String regex = "-";
		
		//Calling method
		String[] strArray = ages.split(regex);
		
		//ergodic
		/*
		for(int x = 0;x < strArray.length;x++) {
			System.out.println(strArray[x]);
		}
		*/
		
		//How to get int type?
		int startAge = Integer.parseInt(strArray[0]);
		int endAge = Integer.parseInt(strArray[1]);
		
		//Keyboard age
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter your age:");
		int age = sc.nextInt();
		
		if(age >= startAge && age <= endAge) {
			System.out.println("You're what I'm looking for");
		}else{
			System.out.println("It doesn't meet my requirements, gun!");
		}
	}
}

Case two:
Separate the following strings:
Note: the path on the hard disk should be replaced with \

public class RegexDemo2 {
	public static void main(String[] args) {
		//Define a string
		String s1 = "aa,bb,cc";
		//Direct segmentation
		String[] str1Array = s1.split(",");
		for(int x = 0;x < str1Array.length;x++) {
			System.out.println(str1Array[x]);
		}
		System.out.println("--------------------");
		
		String s2 = "aa.bb.cc";
		String[] str2Array = s2.split("\\.");
		for(int x = 0;x < str2Array.length;x++) {
			System.out.println(str2Array[x]);
		}
		System.out.println("--------------------");
		
		String s3 = "aa  bb          cc";
		String[] str3Array = s3.split(" +");
		for(int x = 0;x < str3Array.length;x++) {
			System.out.println(str3Array[x]);
		}
		System.out.println("--------------------");
		
		//The path on the hard disk should be replaced by \ \
		String s4 = "E:\\JavaSE\\day14\\avi";
		String[] str4Array = s4.split("\\\\");
		for(int x = 0;x < str4Array.length;x++) {
			System.out.println(str4Array[x]);
		}
		System.out.println("--------------------");
	}
}

Case three:
I have the following string: "91 27 46 38 50"
Please write the code to achieve the final output: "27 38 46 50 91"
Analysis:
A: define a string
B: divide the string to get an array of strings
C: convert string array to int array
D: sort int array
E: reassemble the sorted int array into a string
F: output string
The code implementation is as follows:

public class RegexTest {
	public static void main(String[] args) {
		//Define a string
		String s = "91 27 46 38 50";
		
		//Divide the string to get an array of strings
		String[] strArray = s.split(" ");
		
		//Transform string array into int array
		int[] arr = new int[strArray.length];
		
		for(int x = 0;x < arr.length;x++) {
			arr[x] = Integer.parseInt(strArray[x]);
		}
		
		//Sort int array
		Arrays.sort(arr);
		
		//Assemble the sorted int array into a string
		StringBuilder sb = new StringBuilder();
		for(int x = 0;x < arr.length;x++) {
			sb.append(arr[x]).append(" ");
		}
		//Convert to string
		String result = sb.toString().trim();
		
		//Output string
		System.out.println("result:"+result);
	}
}

Posted by Punk Rock Geek on Sun, 17 Nov 2019 14:35:57 -0800