Menu

Use text of regex match

Help
607
2021-01-19
2021-01-22
  • 607

    607 - 2021-01-19

    Hello, I was expecting this to be one of the most used cases, but I haven't been able to find anyone else doing it or asking about it.
    I would like to replace text using that text. In this specific case, I would like to replace any letter surrounded with square brackets to a lowercased version of the same letter surrounded in triangle brackets (I'm converting BB markup to HTML).
    eg. replace [b] with <b> and [I] with <i>. I have tried for 40 minutes but without success, as I cannot obtain the match's content.
    Thanks!

     
  • Sasumner

    Sasumner - 2021-01-20

    So this is not a Pythonscript question, but search for
    (?i)\[([a-z])\]
    and replace with
    <\l${1}>

     
    • 607

      607 - 2021-01-21

      Thank you! That works, although I'm not quite sure why, because I can't find \l or this use of $ in the documentation for Python's re class.
      Edit: I've been doing some experimenting and was able to use the capitalisation ignore elsewhere too. However, I noticed that the suggested replacement doesn't quite work: [I] becomes <I>. Would it not be possible to let it become <i> automatically? I was intending to use .lower(), but couldn't access the content of the match at all.

       

      Last edit: 607 2021-01-21
  • Sasumner

    Sasumner - 2021-01-22

    You probably have to go to Boost::Regex documentation to find out all the details of the regex syntax used.
    The $ isn't used by itself here. It is part of ${ 1 } which means the first capture group contents.
    In my testing of exactly what I provided, [I] becomes <i> so I can't reproduce what you're saying.
    You can get good N++/Pythonscript regex help by posting here: https://community.notepad-plus-plus.org/

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.