0
0

More than 1 year has passed since last update.

JavaでWindowsのファイル名の禁止文字をアンダースコアに置き換える関数

Last updated at Posted at 2022-07-13

「整数表現が 0 ~ 31 の範囲にある文字」に当てはまる正規表現を脳直で\u0000-\u0031としてしまって、半角スペースも置き換えるようになってしまった。
javaユニコードエスケープは16進数表現であることに気づき修正。


import java.util.regex.Pattern

final val ForbiddenWindowsFilenameCharsRegexPattern = Pattern.compile("""[<>:"/|?*\\\u0000-\u001F]""")

def replaceForbiddenCharWithUnderscore(str: String): String = 
  ForbiddenWindowsFilenameCharsRegexPattern.matcher(str).replaceAll("_")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0