在java中拆分字符串
我有一个字符串的格式: :嗨,这是课文 现在我想在<>符号之间的字符串开始处获取手机号码(不含任何非字母数字字符)即923451234567,以及文本ie Hi here 现在我可以放置一个硬编码逻辑,我现在正在做在java中拆分字符串,java,string,string-split,Java,String,String Split,我有一个字符串的格式: :嗨,这是课文 现在我想在符号之间的字符串开始处获取手机号码(不含任何非字母数字字符)即923451234567,以及文本ie Hi here 现在我可以放置一个硬编码逻辑,我现在正在做 String stringReceivedInSms="<+923451234567>: Hi here is the text."; String[] splitted = cpaMessage.getText().split(">: ", 2); String m
String stringReceivedInSms="<+923451234567>: Hi here is the text.";
String[] splitted = cpaMessage.getText().split(">: ", 2);
String mobileNumber=MyUtils.removeNonDigitCharacters(splitted[0]);
String text=splitted[1];
String stringReceivedInSms=“:嗨,这里是文本。”;
String[]splitted=cpaMessage.getText().split(“>:”,2);
字符串mobileNumber=MyUtils.removeNonDigitCharacters(拆分的[0]);
字符串文本=拆分的[1];
如何使用正则表达式从字符串中整齐地获取所需的字符串?这样我就不必在字符串格式更改时更改代码。使用与模式匹配的正则表达式-
:(*)
使用模式
和匹配器
java类匹配输入字符串
Pattern p = Pattern.compile("<\\+?(\\d+)>: (.*)");
Matcher m = p.matcher("<+923451234567>: Hi here is the text.");
if(m.matches())
{
System.out.println(m.group(1));
System.out.println(m.group(2));
}
Pattern p=Pattern.compile(“:(*)”;
Matcher m=p.Matcher(“:Hi这里是文本。”);
如果(m.matches())
{
系统输出println(m.group(1));
系统输出println(m.group(2));
}
您只需执行以下操作即可获得您的电话号码:
stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">"))
stringReceivedInSms.substring(stringReceivedInSms.indexOf(“”))
因此,请尝试以下代码片段:
public static void main(String[] args){
String stringReceivedInSms="<+923451234567>: Hi here is the text.";
System.out.println(stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">")));
}
publicstaticvoidmain(字符串[]args){
字符串stringReceivedInSms=“:嗨,这里是文本。”;
System.out.println(stringReceivedInSms.substring(stringReceivedInSms.indexOf(“”));
}
您不需要拆分字符串。字符串stringReceivedInSms=“:您好,这是文本。”;
String stringReceivedInSms="<+923451234567>: Hi here is the text.";
Pattern pattern = Pattern.compile("<\\+?([0-9]+)>: (.*)");
Matcher matcher = pattern.matcher(stringReceivedInSms);
if(matcher.matches()) {
String phoneNumber = matcher.group(1);
String messageText = matcher.group(2);
}
Pattern=Pattern.compile(“:(.*)”;
Matcher Matcher=pattern.Matcher(stringReceivedInSms);
if(matcher.matches()){
字符串phoneNumber=matcher.group(1);
字符串messageText=matcher.group(2);
}
您需要使用regex,以下模式将起作用:
^<\\+?(\\d++)>:\\s*+(.++)$
如果
%23
有时也可以出现而不是启动%23
已编码#
,则可能需要先对其进行解码。不用解码,这就是你要找的:(?::(.*)
。非常感谢,你太棒了。你能告诉我从哪里可以学习正则表达式吗?这样我就不会因为这么小的更改而打扰你了。我只需谷歌一下“正则表达式教程”。似乎有互动课程。
public static void main(String[] args) throws IOException {
final String s = "<+923451234567>: Hi here is the text.";
final Pattern pattern = Pattern.compile(""
+ "#start of line anchor\n"
+ "^\n"
+ "#literal <\n"
+ "<\n"
+ "#an optional +\n"
+ "\\+?\n"
+ "#match and grab at least one digit\n"
+ "(\\d++)\n"
+ "#literal >:\n"
+ ">:\n"
+ "#any amount of whitespace\n"
+ "\\s*+\n"
+ "#match and grap the rest of the string\n"
+ "(.++)\n"
+ "#end anchor\n"
+ "$", Pattern.COMMENTS);
final Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
}
923451234567
Hi here is the text.