3

「。」を削除できる正規表現を書きたい。最後に (centa または centb 内にある可能性があります) マークアップし、の前に置きます  

String input1 = "this is a &emsp; <centa> test.</centa>" 
String output1 = "this is a .&emsp;<centa> test</centa>" 

また

 String input1b = "this is a &emsp; <centb> test.</centb>" 
    String output1b = "this is a .&emsp;<centb> test</centb>" 

また

String input3 = "this is a &emsp; test." 
String output3 = "this is a .&emsp; test" 

文字列に対してのみ replaceAll を使用できるので、以下のコードでパターンを作成するにはどうすればよいですか? 置換文字列は何にする必要がありますか?

Pattern rulerPattern1 = Pattern.compile("", Pattern.MULTILINE);
System.out.println(rulerPattern1.matcher(input1).replaceAll(""));

このエッジ ケースは、依頼者がコメントで提示したものです。

string input4 = "&ldquo;[<deleted.material>[</deleted.material>]&sect;&ensp;431:10A&ndash;126&em‌​sp;[<deleted.material>]Chemotherapy services.</deleted.material>] <added.material>Cancer treatment.</added.material>test snl."
string output4 = "&ldquo;[<deleted.material>[</deleted.material>]&sect;&ensp;431:10A&ndash;126.&em‌​sp;[<deleted.material>]Chemotherapy services.</deleted.material>] <added.material>Cancer treatment.</added.material>test snl"
4

3 に答える 3

1

文字列に対してのみreplaceAllを使用できます

わかりました、奇妙な要件ですが、これが私の解決策です。タグの有無にかかわらず、replaceAll を 2 回使用する必要がありました。

private String parse(final String input) {
    return input.replaceAll("this is a &emsp; <(cent(a|b))> test\\.</\\1>", 
        "this is a .&emsp;<$1> test</$1>")
        .replaceAll("&emsp; test.", ".&emsp; test");
}

@Test
public void centa() {
    // Arrange
    final String input = "this is a &emsp; <centa> test.</centa>";

    // Act
    final String output = parse(input);

    // Assert
    assertEquals("this is a .&emsp;<centa> test</centa>", output);
}

@Test
public void centb() {
    // Arrange
    final String input = "this is a &emsp; <centb> test.</centb>";

    // Act
    final String output = parse(input);

    // Assert
    assertEquals("this is a .&emsp;<centb> test</centb>", output);
}

@Test
public void noTags() {
    // Arrange
    final String input = "this is a &emsp; test.";

    // Act
    final String output = parse(input);

    // Assert
    assertEquals("this is a .&emsp; test", output);
}
于 2013-06-12T00:11:29.533 に答える
0

コードを単一の replaceAll と一致させようとしています。これは、3 つのテスト ケースを満たす必要があります。

グループ 1 と 2 は分離されているので、その中にドットを入れることができます。
グループ 2 と 4 は分離されているため、内部のドットを削除できます。

Pattern rulerPattern1 = Pattern.compile("([\\W\\w]+)(&emsp;(<cent[ab]>)?[\\W\\w]+)\\.(</cent[ab]>)?", Pattern.MULTILINE);
System.out.println(rulerPattern1.matcher(input1).replaceAll("$1.$2$4"));
于 2013-06-12T00:24:53.910 に答える