Ken
Ken

Reputation: 39

Notepad++ regexp ID of the sub-expressions in the double parentheses

I am trying to find in the Notepad++ strings like this:

'<a href="/mp3files/cards/Sentence With any Number of Words.mp3"></a>',

And convert them into this:

'<a href="/mp3files/cards/sentence-with-any-number-of-words.mp3"></a>',

I've made a regular expression to crop the string beginning with cards/ and ending with </a>:

(cards/)([^\s]{1,50})(([\s\.\?\!\-\,])(\w{1,50}))+(\.mp3"></a>)

Or an alternative approach:

(cards/)([^\s]{1,50})([\s\.\?\!\-\,]{0,})([^\s]{1,50})

Both work fine for search, but I can't get the replacement. The problem is that the number of words in a sentence may vary. And I can't get the ID of sub-expressions in the double parentheses.

The following format of replacement: \1\2\3... doesn't work, as I can't get the correct ID of the sub-expressions in the double parentheses. I tried to google the topic, but couldn't find anything. Any advice, link or best of all a full replacement expression will be very much appreciated.

Upvotes: 0

Views: 44

Answers (1)

Toto
Toto

Reputation: 91478

This will replace all spaces after /cards/ with a hyphen and lowercase the filename.


  • Ctrl+H
  • Find what: (?:href="/mp3files/cards/|\G)\K(?!\.mp3)(\S+)(?:\h+|(\.mp3))
  • Replace with: \L$1(?2$2:-)
  • CHECK Wrap around
  • CHECK Regular expression
  • Replace all

Explanation:

(?:                     # non capture group
    href="/mp3files/cards/  # literally
  |                       # OR
    \G                      # restart fro last match position
)                       # end group
(?!\.mp3)               # negative lookahead, make sure we haven't ".mp3" after this position
\K                      # forget all we have seen until this position
(\S+)                   # group 1, 1 or more non spaces
(?:                     # non capture group
    \h+                     # 1 or more horizontal spaces
  |                       # OR
    (\.mp3)                 # group 2, literally ".mp3"
)                       # end group

Replacement:

\L$1            # lowercase content of group 1
(?2             # if group 2 exists (the extension .mp3)
    $2              # use it
  :               # else
    -               # put a hyphen
)               # endif

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Upvotes: 1

Related Questions