0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

正規表現で「2個目のカンマ以降を削除」する方法

0
Last updated at Posted at 2026-05-20

自分用備忘録です
例えば、以下のようなCSV風の行があるとき

10000055,3,12
10000326,2,10

これらの行から「2個目のカンマ以降」を削除して、

10000055,3
10000326,2

のように一括で置換したい場合の正規表現の書き方です。


正規表現パターン

^([^,]*,[^,]*),.*

解説

  • ^ :行頭
  • ([^,]*,[^,]*) :1個目のカンマまでの文字列(カンマ以外の文字が続き、カンマ、さらにカンマ以外の文字が続く)のグループ1
  • ,.* :2個目のカンマとその以降のすべての文字列

置換文字列

$1

$1は「グループ1でマッチした部分」の意味。使用環境によっては \1 の場合も。


使用例

元テキスト

10000055,3,12
10000326,2,10

置換後

10000055,3
10000326,2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?