#文字列: 正規表現で全置換

正規表現にマッチする全ての文字列を指定文字列に置換します。たとえばテキスト文中にある全ての電話番号 "0\d{1,4}-\d{1,4}-\d{4}" を「03-0000-0000」に置換できます。

#文字列: 正規表現で全置換

translate #String: Replace All by RegExp

正規表現にマッチする全ての文字列を指定文字列に置換します。たとえばテキスト文中にある全ての電話番号 “0\d{1,4}-\d{1,4}-\d{4}” を「03-0000-0000」に置換できます。

Auto Step icon
Configs for this Auto Step
StrConfA
A: Inputテキストをセットしてください *#{EL}
StrConfB1
B1: 正規表現をセットしてください(例: “0\d{1,4}-\d{1,4}-\d{4}”) *#{EL}
BoolConfB2
B2: 大文字小文字を区別 ⇔ 大文字小文字を無視
StrConfB3
B3: 置換文字列をセットしてください(例: “03-0000-0000″)#{EL}
SelectConfC
C: Outputテキストが格納される文字列型データを選択してください (更新) *
SelectConfD1
D1: Inputテキスト行数を格納する数値型データを選択してください (更新)
Script (click to open)
// Script Example of Business Process Automation
// for 'engine type: 3' ("GraalJS standard mode")
// cf. 'engine type: 2' ("GraalJS Nashorn compatible mode") (renamed from "GraalJS" at 20230526)


//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const strInput       = configs.get       ( "StrConfA" );      // REQUIRED
  if( strInput     === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A: InputText} is empty \n" );
  }
const numInputLines  = strInput.split("\n").length;

const strRegExp      = configs.get       ( "StrConfB1" );     // REQUIRED
const boolIgnoreCase = configs.getObject ( "BoolConfB2" );    // TOGGLE
  // https://questetra.zendesk.com/hc/ja/articles/360024574471-R2300 "Boolean object"
const strReplacement = configs.get       ( "StrConfB3" );     // not required
const strPocketC     = configs.getObject ( "SelectConfC" );   // REQUIRED
const numPocketD1    = configs.getObject ( "SelectConfD1" );  // not required



//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing)



//// == Calculating / 演算 ==
const regSearch  = boolIgnoreCase ?
                   new RegExp( strRegExp, 'ig' ) : new RegExp( strRegExp, 'g' );



//// == Data Updating / ワークフローデータへの代入 ==
/// ref) Retrieving / Updating from ScriptTasks
/// https://questetra.zendesk.com/hc/en-us/articles/360024574771-R2301
/// https://questetra.zendesk.com/hc/ja/articles/360024574771-R2301

if ( strPocketC !== null ){ 
  engine.setData( strPocketC, strInput.replaceAll ( regSearch, strReplacement ) );
}

if ( numPocketD1 !== null ){ 
  engine.setData( numPocketD1, new java.math.BigDecimal( numInputLines ) );
}

} //////// END "main()" /////////////////////////////////////////////////////////////////



/*
### NOTES
- The Process reaches this [Automated Step], the "Replacement" is automatically executed.
    - "Match strings" within the input text are replaced.
- The number of lines in the input text can also be recorded.
- Regular Expressions
    - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions

### NOTES-ja
- この[自動工程]に案件が到達すると、「置換処理」が自動実行されます。
    - Inputテキスト内にある全ての「マッチ文字列」が置換されます。
- 入力Inputテキストの行数も記録可能です。
- 正規表現とは
    - https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Regular_expressions

### Test Data
```
- Tokyo Headquarters
    - Main Line: 03-1234-5678
    - Sales Department: 03-9876-5432
    - Human Resources: 03-1111-2222
    - General Affairs: +81-3-3333-4444
- Osaka Branch
    - Main Line: 06-1234-5678
    - Sales Team 1: 06-8765-4321
```

### APPENDIX
- 正規表現 設定例 / RegExp Example
    - 日本の郵便番号 / Japanese Postal Code
        - `\d{3}-\d{4}`
        - `[0-9]{3}-[0-9]{4}`
        - `(\d{3})-(\d{4})`
        - `(\d{3}-\d{4})`
        - `(\d{3}-\d{4})|(\d{7})`
        - `([0-9]{3}-[0-9]{4})|([0-9]{7})`
    - Email
        - `[\w_.+-]+@[\w-.]+[a-zA-Z]{2,}`
        - `[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-.]+[a-zA-Z]{2,}`
        - `[a-zA-Z0-9_.+-]+@([a-zA-Z0-9-.]+[a-zA-Z]{2,})`
        - `[a-zA-Z0-9_.+-]+@([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,}`
        - `[a-zA-Z0-9_.+-]+@(([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,})`
    - URL
        - `https?://[\w/:%#\$&\?\(\)~\.=\+\-]+`
    - ISO Date from 2024-12-15 to 2025-01-06
        - `(2024-12-1[5-9])|(2024-12-[2-3][0-9])|(2025-01-0[1-6])`
- 正規表現グループ / Groups in regular expressions
    - `(apple|orange)`
        - "apple" か "orange" のいずれか / Either "apple" or "orange"
    - 正規表現は複数のキャプチャグループを持つことができます。
        - キャプチャグループ内の左括弧と同じ順にある、配列要素に一致します。
    - A regular expression may have multiple capturing groups.
        - Matches to capturing groups in an array whose members are in the same order as the left parentheses
- 正規表現文字クラス / A character class
    - `[xyz]`
        - "x" か "y" か "z" の 一文字 / Any one of the enclosed characters, "x" "y" or "z"
    - `[a-c]`
        - "a" から "c" までのいずれか一文字 / Any one in the range "a" to "c"
    - `[^xyz]`
        - "x" でも "y" でも "z" でもない一文字 / Any one that is neither "x" nor "y" nor "z"
    - `\d`
        - 数字一文字 / Any digit / `[0-9]`
    - `\w`
        - 半角英数字一文字 / Any alphanumeric character / `[A-Za-z0-9_]`
    - `\t`
        - 水平タブ / A horizontal tab
    - `.`
        - 改行文字を除くあらゆる一文字 / Any single character except line terminators
- 正規表現アサーション言明 / Assertions
    - `^`
        - 先頭 / Beginning
    - `$`
        - 末尾 / End
    - `\b`
        - 区切り / A word boundary
    - `\B`
        - 区切り以外 / A non-word boundary
- 正規表現数量詞 / Quantifiers
    - `x*`
        - 直前アイテム "x" の0回以上の繰返 / The preceding item "x" 0 or more times
    - `x+`
        - 直前アイテム "x" の1回以上の繰返 / The preceding item "x" 1 or more times
    - `x?`
        - 直前アイテム "x" の0回か1回の出現 / The preceding item "x" 0 or 1 times
    - `x{n}`
        - 直前アイテム "x" のn回の出現 / "n" occurrences of the preceding item "x"
    - `x{n,m}`
        - 直前アイテム "x" がnからm回出現 / at least "n" and at most "m" occurrences
*/

Download

warning 自由改変可能な JavaScript (ECMAScript) コードです。いかなる保証もありません。
(アドオン自動工程のインストールは Professional editionでのみ可能です)

Notes

Capture

Test Data

- Tokyo Headquarters
    - Main Line: 03-1234-5678
    - Sales Department: 03-9876-5432
    - Human Resources: 03-1111-2222
    - General Affairs: +81-3-3333-4444
- Osaka Branch
    - Main Line: 06-1234-5678
    - Sales Team 1: 06-8765-4321

Appendix

  • 正規表現 設定例 / RegExp Example
    • 日本の郵便番号 / Japanese Postal Code
      • \d{3}-\d{4}
      • [0-9]{3}-[0-9]{4}
      • (\d{3})-(\d{4})
      • (\d{3}-\d{4})
      • (\d{3}-\d{4})|(\d{7})
      • ([0-9]{3}-[0-9]{4})|([0-9]{7})
    • Email
      • [\w_.+-]+@[\w-.]+[a-zA-Z]{2,}
      • [a-zA-Z0-9_.+-]+@[a-zA-Z0-9-.]+[a-zA-Z]{2,}
      • [a-zA-Z0-9_.+-]+@([a-zA-Z0-9-.]+[a-zA-Z]{2,})
      • [a-zA-Z0-9_.+-]+@([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,}
      • [a-zA-Z0-9_.+-]+@(([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,})
    • URL
      • https?://[\w/:%#\$&\?\(\)~\.=\+\-]+
    • ISO Date from 2024-12-15 to 2025-01-06
      • (2024-12-1[5-9])|(2024-12-[2-3][0-9])|(2025-01-0[1-6])
  • 正規表現グループ / Groups in regular expressions
    • (apple|orange)
      • “apple” か “orange” のいずれか / Either “apple” or “orange”
  • 正規表現文字クラス / A character class
    • [xyz]
      • “x” か “y” か “z” の 一文字 / Any one of the enclosed characters, “x” “y” or “z”
    • [a-c]
      • “a” から “c” までのいずれか一文字 / Any one in the range “a” to “c”
    • [^xyz]
      • “x” でも “y” でも “z” でもない一文字 / Any one that is neither “x” nor “y” nor “z”
    • \d
      • 数字一文字 / Any digit / [0-9]
    • \w
      • 半角英数字一文字 / Any alphanumeric character / [A-Za-z0-9_]
    • \t
      • 水平タブ / A horizontal tab
    • .
      • 改行文字を除くあらゆる一文字 / Any single character except line terminators
  • 正規表現アサーション言明 / Assertions
    • ^
      • 先頭 / Beginning
    • $
      • 末尾 / End
    • \b
      • 区切り / A word boundary
    • \B
      • 区切り以外 / A non-word boundary
  • 正規表現数量詞 / Quantifiers
    • x*
      • 直前アイテム “x” の0回以上の繰返 / The preceding item “x” 0 or more times
    • x+
      • 直前アイテム “x” の1回以上の繰返 / The preceding item “x” 1 or more times
    • x?
      • 直前アイテム “x” の0回か1回の出現 / The preceding item “x” 0 or 1 times
    • x{n}
      • 直前アイテム “x” のn回の出現 / ”n” occurrences of the preceding item “x”
    • x{n,m}
      • 直前アイテム “x” がnからm回出現 / at least “n” and at most “m” occurrences

See Also

上部へスクロール

Questetra Supportをもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む