<java> Split the address before and after the street address with a regular expression

Overview

I'm going to use a regular expression in java to split the address before and after the street address.

By entering the address from the console, the split address will be displayed. * Please note that this is just a simple matter and not all addresses can be completely divided. </ font>

procedure

The procedure is as follows

  1. Compile the regular expression with the Pattern class (java.util.regex.Pattern)
  2. Check if the character string entered by the find method of the Matcher class (java.util.regex.Matcher) matches the regular expression.
  3. If there is a match, the characters are split and displayed.

For details on regular expressions, refer to this site. https://www.sejuku.net/blog/13215

code

SplitAddress.java



import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SplitAddress{

	public static void main(String args[]) throws Exception {
		System.out.println("Please enter your address");
		Scanner sc = new Scanner(System.in);
		//Pass the characters entered in the console as arguments to the method below.
		String[] ad = split(sc.nextLine());

		//Output
		System.out.println(ad[0]+"\n"+ad[1]+"\n"+ad[2]);
		sc.close();
	}

	//Method to split address
	static String[] split(String str)throws Exception {
		String[] ad = new String[3];

		//Preparation for generation of judgment pattern
		//Half-width and full-width numbers are at least once or Chinese numerals are at least once (at the beginning of the address)
		String p1 = "([0-90-9]+|[1 2 3 4 5 6 7 8 9 million]+)+";

		//Half-width and full-width numbers are 1 or more, Chinese numerals are 1 or more, and connection symbols such as hyphens are 0 or more (contents of address)
		String p2 = "([0-90-9]+|[1 2 3 4 5 6 7 8 9 million]+|(Chome|Ding|address|Turn|issue|-|‐|-|−|of))*";

		//Half-width numbers / full-width numbers more than once or Chinese numerals more than once or numbers/The character used at the end of the address such as the number is once (the end of the address)
		String p3 = "([0-90-9]+|[1 2 3 4 5 6 7 8 9 million]+|(Chome|Ding|address|Turn|issue))";

		//ex)Address is 2-6-If 6, "2" is p1, "-6-"Applies to p2 and" 6 "applies to p3.

		//Regular expressions
		Pattern p = Pattern.compile(p1+p2+p3);
		Matcher m = p.matcher(str);

		//If there is a character string that corresponds to the regular expression, split the address before and after it
		if(m.find()) {
			ad[0] = str.substring(0,m.start());
			ad[1] = str.substring(m.start(),m.end());
			ad[2] = str.substring(m.end(),str.length());


			//Shimanto 3-6-Chinese numerals like 1+Processing for the time of numbers
			//(If there is a character string corresponding to the regular expression of 1 Chinese numeral + 1 digit in the address part of the address divided into 3 parts, correct the boundary of division.)

			//1 Chinese numeral + 1 number
			String p4 = "[1 2 3 4 5 6 7 8 9 million][0-90-9]";

			p = Pattern.compile(p4);
			m = p.matcher(ad[1]);
			if(m.find()) {
				String tmp = ad[1].substring(0,m.start());
				ad[0] = ad[0] + tmp;
				ad[1] = ad[1].substring(m.start(),ad[1].length());
			}

			//3-4-5 Numbers like Mitsui Building+Processing for the time of Chinese numerals
			String p5 = "[0-90-9][1 2 3 4 5 6 7 8 9 million]";
			p = Pattern.compile(p5);
			m = p.matcher(ad[1]);
			if(m.find()) {
				String tmp = ad[1].substring(m.start()+1,ad[1].length());
				ad[2] = tmp + ad[2];
				ad[1] = ad[1].substring(0,m.start());
			}
		}else ad[0] = str;

		return ad;
	}
}

Execution result

The address entered in green letters and the address divided into three lines below it.

image.png

Try it out! !! </ font>

Recommended Posts