如何使用正则表达式删除不需要的空格和字符串的最后一个单词?

时间:2021-06-01 21:41:53

I have a string that contains words in a pattern like this:

我有一个字符串,其中包含如下模式中的单词:

  2013-2014  XXX 29 
  2011-2012  XXXX 44

Please note that there are 2 whitespaces before AND after the year. I need to remove the first 2 whitespaces, the 1 whitespace after the year and the last word (29/44 etc). So it will become like this:

请注意,在年之前和之后有2个空格。我需要删除前两个空格,一年后的1个空格和最后一个字(29/44等)。所以它会变成这样:

2013-2014 XXX
2011-2012 XXXX

Im really bad with Regex so any help would be appreciated. So far i can remove the last word with

我真的很不喜欢正则表达式,所以任何帮助将不胜感激。到目前为止,我可以删除最后一个单词

str.replaceAll(" [^ ]+$", "");

4 个解决方案

#1


1  

Select only what you want and replace the rest (with a space in the middle) :) This should work for you :

只选择你想要的东西并替换其余的东西(中间有一个空格):)这应该适合你:

public static void main(String[] args) throws IOException {
    String s1 = "  2013-2014  XXX 29 ";
    System.out.println(s1.replaceAll("^\\s+([\\d-]+)\\s+(\\w+).*", "$1 $2"));

    String s2 = "  2011-2012  XXXX 44 ";
    System.out.println(s2.replaceAll("^\\s+([\\d-]+)\\s+(\\w+).*", "$1 $2"));
}

O/P :

2013-2014 XXX
2011-2012 XXXX

#2


1  

You can use a single regex for this:

您可以使用单个正则表达式:

str = str.replaceAll("^ +|(?<=\\d{4} ) | [^ ]+ *$", "");

RegEx Demo

RegEx Breakup:

^ +             # 1 or more spaces at start
|               # OR
(?<=\\d{4} )    # space after 4 digit year and a space
|               # OR
 [^ ]+ *$       # text after last space at end

#3


0  

you could also do it in multiple more easy to understand steps, like this:

您还可以通过多个更易于理解的步骤来执行此操作,例如:

public static void main(String[]args){
    String s = "  2011-2012  XXXX 44";
    // Remove leading and trailing whitespace
    s = s.trim();
    System.out.println(s);
    // replace two or more whitespaces with a single whitespace
    s = s.replaceAll("\\s{2,}", " ");
    System.out.println(s);
    // remove the last word and the whitespace before it
    s = s.replaceAll("\\s\\w*$", "");
    System.out.println(s);
}

O/P:

2011-2012  XXXX 44
2011-2012 XXXX 44
2011-2012 XXXX

#4


0  

You can also try this:

你也可以试试这个:

str = str.replaceAll("\\s{2}", " ").trim();

Example:

String str = "  2013-2014  XXX 29 ";

Now:

str.replaceAll("\\s{2}", " ");

Output: " 2013-2014 XXX 29 "

输出:“2013-2014 XXX 29”

And with .trim() it looks like this: "2013-2014 XXX 29"

并且.trim()看起来像这样:“2013-2014 XXX 29”

#1


1  

Select only what you want and replace the rest (with a space in the middle) :) This should work for you :

只选择你想要的东西并替换其余的东西(中间有一个空格):)这应该适合你:

public static void main(String[] args) throws IOException {
    String s1 = "  2013-2014  XXX 29 ";
    System.out.println(s1.replaceAll("^\\s+([\\d-]+)\\s+(\\w+).*", "$1 $2"));

    String s2 = "  2011-2012  XXXX 44 ";
    System.out.println(s2.replaceAll("^\\s+([\\d-]+)\\s+(\\w+).*", "$1 $2"));
}

O/P :

2013-2014 XXX
2011-2012 XXXX

#2


1  

You can use a single regex for this:

您可以使用单个正则表达式:

str = str.replaceAll("^ +|(?<=\\d{4} ) | [^ ]+ *$", "");

RegEx Demo

RegEx Breakup:

^ +             # 1 or more spaces at start
|               # OR
(?<=\\d{4} )    # space after 4 digit year and a space
|               # OR
 [^ ]+ *$       # text after last space at end

#3


0  

you could also do it in multiple more easy to understand steps, like this:

您还可以通过多个更易于理解的步骤来执行此操作,例如:

public static void main(String[]args){
    String s = "  2011-2012  XXXX 44";
    // Remove leading and trailing whitespace
    s = s.trim();
    System.out.println(s);
    // replace two or more whitespaces with a single whitespace
    s = s.replaceAll("\\s{2,}", " ");
    System.out.println(s);
    // remove the last word and the whitespace before it
    s = s.replaceAll("\\s\\w*$", "");
    System.out.println(s);
}

O/P:

2011-2012  XXXX 44
2011-2012 XXXX 44
2011-2012 XXXX

#4


0  

You can also try this:

你也可以试试这个:

str = str.replaceAll("\\s{2}", " ").trim();

Example:

String str = "  2013-2014  XXX 29 ";

Now:

str.replaceAll("\\s{2}", " ");

Output: " 2013-2014 XXX 29 "

输出:“2013-2014 XXX 29”

And with .trim() it looks like this: "2013-2014 XXX 29"

并且.trim()看起来像这样:“2013-2014 XXX 29”