LoginSignup
2
2

More than 3 years have passed since last update.

正規表現で「先頭ではない」「行末ではない」を表現

Last updated at Posted at 2020-10-19

やりたいこと

先頭ではない|を置換したい。
Markdownの表をHTMLに変えたかった。
Markdown表変換ツール - MarkDown Convertを使うという手もあります。

正規表現

参考 : 正規表現で、「先頭にある」文字列ではなく、「先頭にはない」文字列だけにマッチさせたい場合 - Sakura scope

(.)\|(.)

考え方

否定を考えるのではなく^\|(先頭の|)じゃなくて直前に任意の1文字.がある\|と考える。
.\|.ではなく(.)\|(.)とするのは直前にある任意の1文字.を置換後の文字で\1 \2として削除されないようにする。
hoge|fugahoge</td><td>fugaにしたくて.\|.で置換後文字を</td><td>にするとhog</td><td>ugaになって、.に相当するgfが削除される。

置換前
|head1|head2|head3|
|:-:|:-:|:-:|
|hoge|fuga|poo|
|hoge|fuga|poo|
|hoge|fuga|poo|
置換対象 置換後
(.)\|(.) \1</td><td>\2
変換後
|head1</td><td>head2</td><td>head3|
|:-:</td><td>:-:</td><td>:-:|
|hoge</td><td>fuga</td><td>poo|
|hoge</td><td>fuga</td><td>poo|
|hoge</td><td>fuga</td><td>poo|
2
2
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
2
2