package com.inspur.iaistelnumber.service.impl;
import com.inspur.iaistelnumber.entity.TelData;
import com.inspur.iaistelnumber.entity.TelInfo;
import com.inspur.iaistelnumber.service.TelService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Service
public class TelServiceImpl implements TelService {
@Override
public TelData identify(String words) {
String regex = "(13[0-9]{9}|14[0-14-9]{9}|15[0-35-9]{9}|16[2567]{9}|17[0-8]{9}|18[0-9]{9}|19[0-35-9]{9})";
String regex1 = "\\d{3,4}-\\d{7,8}";
int count = 0;
List<TelInfo> list = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(words);
while (matcher.find()) {
if (words.length() == matcher.end()) {
TelInfo info = new TelInfo();
count++;
info.setBegin(matcher.start());
info.setEnd(matcher.end());
info.setNum(matcher.group());
info.setType("手机号码");
list.add(info);
}else if (words.charAt(matcher.end()) < 48 || words.charAt(matcher.end()) > 57) {
TelInfo info = new TelInfo();
count++;
info.setBegin(matcher.start());
info.setEnd(matcher.end());
info.setNum(matcher.group());
info.setType("手机号码");
list.add(info);
}
}
return null;
}
}
Java 识别字符串中的手机号
于 2022-07-23 16:02:46 首次发布