🖥

Example of making text hashtag style of Twitter with # regular express

2019/04/20に公開

<ul><li> Match and replace at word boundaries <code>\b</code> </li></ul><h1> All words (roughly) </h1><pre> <code class="py">>>> re.sub(r'\b([A-Za-z][A-Za-z0-9]+)\b', "#\1", "A whole new-world under_the_sea") 'A #whole #new-#world under_the_sea'</code> </pre><h1> Specified word </h1><p> <code>perlpythonruby</code> that the string <code>perlpythonruby</code> is not replaced </p><pre> <code class="py">>>> re.sub(r'\b(ruby|perl|python)\b', "#\1", "Do you like python or ruby? mixed perlpythonruby languate does not exist?") 'Do you like #python or #ruby? mixed perlpythonruby languate does not exist?'</code> </pre><h1> Case-insensitive pattern </h1><p> You can see that the upper case Ruby is also replaced </p><pre> <code class="py">>>> pattern = re.compile(r'\b(ruby|perl|python)\b', re.IGNORECASE) >>> re.sub(pattern, "#\1", "Do you like python or Ruby? mixed perlpythonruby languate does not exist?") 'Do you like #python or #Ruby? mixed perlpythonruby languate does not exist?'</code> </pre><h1> Example of ignoring an existing hashtag </h1><p> Use a negative look back <code>?<!#</code> </p><pre> <code class="py">>>> re.sub(r'\b(?<!#)(ruby|perl|python)\b', "#\1", "Do you like #python or ruby? mixed perlpythonruby languate does not exist?") 'Do you like #python or #ruby? mixed perlpythonruby languate does not exist?'</code> </pre><h1> All inclusive </h1><pre> <code class="py">>>> pattern = re.compile(r'\b(?<!#)(ruby|perl|python)\b', re.IGNORECASE) >>> re.sub(pattern, "#\1", "Do you like #python or ruby or Perl? mixed perlpythonruby languate does not exist?") 'Do you like #python or #ruby or #Perl? mixed perlpythonruby languate does not exist?'</code> </pre>

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/1342

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-20

Discussion