给定一个字符串在第50个位置插入一个新的换行符并排除整个单词

时间:2018-05-02 17:59:41

标签: java regex

'Someone <someone@mysite.com>'

我需要字符串new string

String str = "Hello how are you today music fine.This is a new line"
String parsedStr = str.replace("(.{50})","$1\n");

但我得到这样的

Hello how are you today music fine.This is a new 
line

我遇到了单词边界(\ b)。尝试但不适用于我的情况。

4 个答案:

答案 0 :(得分:2)

使用每行使用的字符数构造正则表达式.{1,N} 与50一样,它将是.{1,50}

压缩

查找:(?:(?:(?>(.{1,50})(?:(?<=[^\S\r\n])[^\S\r\n]?|(?<=[,.;:!/?])[^\S\r\n]?|(?=\r?\n|[-#%&*@_])|[^\S\r\n]))|(.{1,50}))(?:\r?\n)?|(?:\r?\n))

替换:$1$2\r\n

可读

 (?:
      # -- Words/Characters 
      (?:
           (?>                           # Atomic Group - Match words with valid breaks
                ( .{1,50} )                   # (1), 1-N characters
                                              #  Followed by one of 4 prioritized, non-linebreak whitespace
                (?:                           #  break types:
                     (?<= [^\S\r\n] )              # 1. - Behind a non-linebreak whitespace
                     [^\S\r\n]?                    #      ( optionally accept an extra non-linebreak whitespace )
                  |  (?<= [,.;:!/?] )              # 2. - Behind sepcial punctuation breaks
                     [^\S\r\n]?                    #      ( optionally accept an extra non-linebreak whitespace )
                  |  (?=                           # 3. - Ahead a linebreak or special punctuation breaks
                          \r? \n 
                       |  [-#%&*@_] 
                     )
                  |  [^\S\r\n]                     # 4. - Accept an extra non-linebreak whitespace
                )
           )                             # End atomic group
        |  
           ( .{1,50} )                   # (2), No valid word breaks, just break on the N'th character
      )
      (?: \r? \n )?                 # Optional linebreak after Words/Characters
   |  
      # -- Or, Linebreak
      (?: \r? \n )                  # Stand alone linebreak
 )

答案 1 :(得分:1)

您的问题很可能是一个简单的解决方案。看看OP如何只为我们提供了一个示例字符串,不确定是否存在任何特殊情况,但以下内容适用于其示例字符串。

注意:我发布了 raw 正则表达式。您将需要在Java中转义所有必需的字符(反斜杠)。

选项1

您甚至可以使用^(.{0,50})(末尾有空格)而不是\b

See regex in use here

^(.{0,50}\b)
  • ^在行首处断言位置
  • (.{0,50}\b)捕获最多50次的任何字符,声明结束位置与字边界\b匹配到捕获组1

替换:$1\n

结果:

Hello how are you today music fine.This is a new 
line

选项2

此方法使用选项1作为基础,但为字符串添加逻辑,可在标点符号之前添加\n可能是非预期的结果)。对于这些情况,您可以使用以下内容。

See regex in use here

^(.{0,50}\b(?!\p{P}|$))
  • 与选项1相同的逻辑,但确保\b\p{p}(标点符号)或字符串结尾不匹配。

结果(第二个选项在使用选项1的正则表达式\n之前.}:

Hello how are you today music fine.This is a new 
line
This line contains 50 characters to show dot 
issue.Some other string here

答案 2 :(得分:0)

试试这段代码:

String str = "Hello how are you today music fine.This is a new line"
int index = 50;
while(str.getCharAt(index) != ' ') 
{
    index--;
}

String parsedStr = str.replace("(.{index})","$1\n");

答案 3 :(得分:0)

字面你所要求的,仅此而已。但是,如果你真的想要实现自动换行,那么这不是一个好方法。

String str = "Hello how are you today music fine.This is a new line";
String parsedStr = str.replaceFirst("^(.*?)\\s*((\\S(?!\\s))*(?<=.{50}).*$)",
        "$1"+System.lineSeparator()+"$2");
String [] pieces = parsedStr.split(System.lineSeparator());
for(String piece : pieces ) {
    System.out.format("length %2d: \"%s\"%n", piece.length(), piece);
}

<强>输出:

  

长度48:“你好,今天音乐如何精美。这是一个新的”

     

长度4:“行”

<强>解释

  • ^ =行首(开始第1组)
  • .*? =不情愿地匹配任意数量的字符(结束组1)
  • \s* =消耗线将分割的所有空格(开始第2组)
  • \S* =匹配任意数量的非空白字符......
  • (?!\\s) =后面没有空格,否则我们会错过50分的机会
  • (?<=.{50}) =正好50个字符的正面背后
  • .*$ =包括该行中的所有剩余字符(结束组2)

更简单的选项:

使用这个更简单的正则表达式可以得到非常相似的结果:

String parsedStr = str.replaceFirst("^(.{0,50})\\s+",
        "$1"+System.lineSeparator());

(对ctwheels提供类似建议的信用)

唯一的区别是,如果在拆分之前有多个空格,那么在第1行的末尾可能会有一些额外的空格。但这不是一件坏事。