Example of making text hashtag style of Twitter with # regular express
<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
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。
公開日時
2019-04-20
Discussion