Select the last N characters of selected line
-
Hello!
this command:
^(?=.*Location="file://localhost/C:/Users/nilsn/Music/DJ%20Master%20Folder/JOBB/Hits%20Snerikes%202/)(?=.*.mp3).*$
helps me find the lines i need to change in my XML document,
now the lines all end with: .mp3"
and i want to replace that (the last 5 characters of every line) with .aiff"how do i select the last 5 characters of the select statement above? (now it selects the whole line)
----
moderator added code tags to make your regex readable -
EDIT:
i found this statement to select the last 5 digits of line:
(.{0,5})$
how do i combine it with my first statement?
-
@L-S,
^(?=.*Location="file://localhost/C:/Users/nilsn/Music/DJ%20Master%20Folder/JOBB/Hits%20Snerikes%202/)(?=.*.mp3).*$
…
how do i combine it with my first statement?As written? You don’t (or, at least, not easily): This is because your first regex uses lookaheads, whereas wanting the last 5 characters will require that what goes before are using lookbehinds instead. And lookbehinds can’t be variable width, so it makes it more complicated: you could use
\K
instead, but that only works with Replace All. (All these concepts are described in detail in the manual; see the link to the user manual below, then search for “lookahead” or “lookbehind” or “\K” to find those specific topics). However, that seems overly complicated for the goal you have.I would personally try just using FIND =
\.mp3"
and REPLACE =.aiff"
in regular expression mode (or simpler,.mp3"
with.aiff"
in normal mode), assuming that there aren’t other strings in your document that include.mp3"
…If there are other instances of
.mp3"
that you don’t want to match (ie, not in the location fields), then I would use something like FIND =(Location="\S+?)\.mp3"
and REPLACE =$1.aiff"
– the()
will put most of the location into group1 (instead of dealing with lookaheads/lookbehdinds/\K
, which are more difficult to get right than capture groups); the\S+?
grabs as few non-space characters (\S
means “non-space” means “not a space or tab or newline or similar” – again, search the usermanual link below for “\S”); putting the\
before the.
in\.mp3"
will make sure that the dot is matched literally (search for “literal”), so thatLocation="blahxmp3"
will not match my regex, where it would match your regex.If that doesn’t work for you, then please post additional data using the Template for Search/Replace questions, which follows the recommendations of the Formatting Forum Posts FAQ to make your question more readable (the asterisks from your original regex will make text italic in the forum, making us have to guess what your actual regex text is; that’s why we have those FAQs, and the “Read This Before Posting” at the top of Help Wanted and General Discussion, which tells you to read those other two FAQs, to make sure that regex questions are understandable, and written in such a way that makes it easier for us to help you)
----
Useful References
-
@L-S ,
Thinking about it some more after I posted, and realizing that maybe the extra parts of the file URL were important – you only want to replace the
.mp3"
with.aiff"
for a specific subdirectory, like you show: if so, you actually already almost had it with your original:^(?=.*Location="file://localhost/C:/Users/nilsn/Music/DJ%20Master%20Folder/JOBB/Hits%20Snerikes%202/)(?=.*.mp3).*$
If you just removed the “lookahead” portion of the first group and the caret to anchor it to the beginning:
(Location="file://localhost/C:/Users/nilsn/Music/DJ%20Master%20Folder/JOBB/Hits%20Snerikes%202/)(?=.*.mp3).*$
… and then move the
.*
into the first group rather than the second(Location="file://localhost/C:/Users/nilsn/Music/DJ%20Master%20Folder/JOBB/Hits%20Snerikes%202/.*)(?=.mp3).*$
… and then make the
.mp3"
be the ending, rather than a lookahead for .mp3 and matching the whole line:(Location="file://localhost/C:/Users/nilsn/Music/DJ%20Master%20Folder/JOBB/Hits%20Snerikes%202/.*)\.mp3"
Then my replacement of
$1.aiff"
would also work for you. -
Wow thanks for a quick and helpful reply!
Yes, i only want to change the ending of the files located in a certain folder:
Location="file://localhost/C:/Users/nilsn/Music/DJ%20Master%20Folder/JOBB/Hits%20Snerikes%202/
None of the other 4000 mp3 files in the XML are supposed to be changed. However they all have location in their line, but not the specific folder (/Hits%20Snerikes%202/) which only 280 files has (which are the only ones i wanna change)
now how would you modify your statement: (Location=“\S+?).mp3”
so that it only selects the line with “/Hits%20Snerikes%202/” in location, and then replace the last 5 characters of that line (.mp3") with .aiff"
-
@PeterJones said in Select the last N characters of selected line:
(Location=“file://localhost/C:/Users/nilsn/Music/DJ%20Master%20Folder/JOBB/Hits%20Snerikes%202/.*).mp3”
BINGO!
That worked.
Thanks a bunch dude