在Java中,用于移除末尾数字(包括连字符符号)的正则表达式为:

huangapple go评论84阅读模式
英文:

Regular Expression in Java to remove the trailing digits including Hyphen symbol

问题

我有很多像下面这样的9位数字的邮政编码。

    94107-1532
    94107-1532
    94107-1535
    94107-1511

这九位邮政编码的前面部分是邮政编码的前五位数字,表示目标邮局或投递区域。九位邮政编码的最后4位数字代表该整个投递区域内的特定投递路线。我想去掉包括连字符(`-`)在内的最后4位数字。我尝试了下面的表达式,但没有成功!

    public static String removeRoute(String zipCode) {
        return zipCode.replaceAll("-\\d$", "");
    }
英文:

I’ve lots of 9-digit zip codes like shown below.

94107-1532
94107-1532
94107-1535
94107-1511

The first part is the first five digits of the zip code which indicates the destination post office or delivery area. The last 4 digits of the nine-digit ZIP Code represents a specific delivery route within that overall delivery area. I wanted to remove the last 4 digits starting including the Hyphen symbol(-). I tried the below expression, but no luck!

public static String removeRoute(String zipCode) {
    return zipCode.replaceAll("-\\d$", "");
}

答案1

得分: 2

\\d只匹配一个数字字符。

使用\\d+来匹配多个数字,或者使用\\d{4}来精确匹配四个。

英文:

\\d only matches one digit character.

Use \\d+ to match multiple or \\d{4} to match exactly four

答案2

得分: 2

我甚至不会使用正则表达式;只需使用String#split

String input = "94107-1532";
String zip = input.split("-")[0];
String poBox = input.split("-")[1];
英文:

I wouldn't even use regex; just use String#split:

String input = "94107-1532";
String zip = input.split("-")[0];
String poBox = input.split("-")[1];

答案3

得分: 1

你正在匹配字符串末尾的连字符和单个数字。

更精确的匹配方法是捕获前5位数字并匹配末尾的连字符和4位数字。在替换中使用第1组。

^(\d{5})-\d{4}$

正则表达式演示

或者如果该模式只出现在字符串末尾,你可以使用单词边界,例如:

\b(\d{5})-\d{4}$

示例代码:

public static String removeRoute(String zipCode) {
    return zipCode.replaceAll("^(\\d{5})-\\d{4}$", "$1");
}
英文:

You are matching a hyphen and a single digit at the end of the string.

A bit more precise match would be to capture the first 5 digits in a group and match the hyphen and 4 digits at the end. In the replacement use group 1

^(\d{5})-\d{4}$

Regex demo

Or if the pattern can only occur at the end of the string, you can use for example a word boundary

\b(\d{5})-\d{4}$

Example code

public static String removeRoute(String zipCode) {
	return zipCode.replaceAll("^(\\d{5})-\\d{4}$", "$1");
}

答案4

得分: 0

你可以在\d简写之后直接使用星号加号

zipCode.replaceAll("-\\d+$", "");

或者

zipCode.replaceAll("-\\d*$", "");

星号(*)表示引擎会尝试零次或多次匹配前面的标记,加号(+)表示引擎会尝试一次或多次匹配前面的标记。

英文:

You can use an asterisk or plus sign right after the \d shorthand.

zipCode.replaceAll("-\\d+$", "");

OR

zipCode.replaceAll("-\\d*$", "");

The asterisk or star (*) tells the engine to attempt to match the preceding token zero or more times and the plus (+)tells the engine to attempt to match the preceding token once or more.

huangapple
  • 本文由 发表于 2020年8月30日 17:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63655856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定