Class Sed

java.lang.Object
com.nefrock.edgeocr.Sed

public class Sed extends Object
sed ライクなテキスト変換機能を提供するユーティリティクラスです。

入力文字列に対してパターン検索と置換を行い、変換後の文字列を返します。 パターンはリテラル文字列として扱うことも、正規表現として扱うこともできます。 置換はマッチ箇所すべてに適用されます。

戻り値が null になる主なケース:

  • 引数 input / pattern / replacement のいずれかが null の場合
  • 内部変換処理でエラーが発生した場合(例: 不正な正規表現)

使用例:

 // 1) リテラル置換(全マッチ)
 String s1 = Sed.transform("foo bar foo", "foo", "qux");
 // -> "qux bar qux"

 // 2) 正規表現置換(数字を除去)
 String s2 = Sed.transformRegex("Item1: $25.99", "\\d", "");
 // -> "Item: $."

 // 3) キャプチャグループを使った並び替え
 String s3 = Sed.transformRegex("Yamada, Taro", "(\\w+),\\s*(\\w+)", "$2 $1");
 // -> "Taro Yamada"
 
  • Constructor Details

    • Sed

      public Sed()
  • Method Details

    • transform

      public static String transform(String input, String pattern, String replacement)
      文字列変換を実行します(リテラルパターン)。

      このメソッドは内部で useRegex = falsetransform(String, String, String, boolean) を呼び出します。

      Parameters:
      input - 変換対象の文字列
      pattern - 検索するパターン(リテラル文字列)
      replacement - 置換文字列
      Returns:
      変換後の文字列。エラー時は null
    • transform

      public static String transform(String input, String pattern, String replacement, boolean useRegex)
      文字列変換を実行します。

      useRegex = true の場合、pattern は正規表現として評価されます。 useRegex = false の場合、pattern はリテラル文字列として扱われます。

      正規表現モードでは、置換文字列に $1, $2 のような キャプチャ参照を使用できます。

      引数のいずれかが null の場合は変換を行わず、null を返します。

      Parameters:
      input - 変換対象の文字列
      pattern - 検索するパターン
      replacement - 置換文字列
      useRegex - trueの場合、patternを正規表現として扱います。falseの場合、リテラル文字列として扱います。
      Returns:
      変換後の文字列。エラー時は null
    • transformRegex

      public static String transformRegex(String input, String regex, String replacement)
      文字列変換を実行します(正規表現パターン)。

      このメソッドは内部で useRegex = truetransform(String, String, String, boolean) を呼び出します。

      Parameters:
      input - 変換対象の文字列
      regex - 検索する正規表現パターン
      replacement - 置換文字列
      Returns:
      変換後の文字列。エラー時は null