java要怎么实现当读到某个特定字符时,将该字符前的字符串读出来呢

碰到一个要解析字符串的问题,有几个组成结构相同的字符串拼接在一起每条字符串中包含的数据都有多个不同的特定字符隔开,比如有分号和换行符,然后要从整个拼接的字符串中解析出所需的数据,java要怎么实现当读到某个特定字符时,将该字符前的字符串读出来呢

正则表达式
举例:需要提取车牌号中最后一个数字,比如说:苏A7865提取5,苏A876X提取6

实现方法:


import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Test {

public static void main(String[] args) {

String s = "A876X";

// 把要匹配的字符串写成正则表达式,然后要提取的字符使用括号括起来

// 在这里,我们要提取最后一个数字,正则规则就是“一个数字加上大于等于0个非数字再加上结束符”

Pattern pattern = Pattern.compile("(\\d)[^\\d]*$");

Matcher matcher = pattern.matcher(s);

if(matcher.find())

System.out.println(matcher.group(1));

}

}

正则表达式

split

split || 正则表达式