Hope this helps or clarifies your problem, apologies if it doesn't.Is there also a solution for "UltraEdit-32"? -C167 Again you may be able to use it to store all unique instances and utilize that somehow. If you really wanted to find a way to automate this to make it work, use /(\b : )/g to match every instance of xx:, store them all in an array and if there is a duplicate, run the original regex on that specific one to continue trimming it down until there are no more duplicates. In my example I use lookahead instead so it checks if there are any instances of it ahead, if so ignore the current match (so it selects only the last instance). I could not personally find any regex debuggers that accept backreferencing and lookbehind. I may be wrong, if someone can find an online regex debugger that supports lookbehind AND backreferencing, let me know and I'll see if I can write an expression to work. My conclusion is that I don't believe you can do this all with regex. Only issue with this is that it'll still match the last instance even if it's the only instance. This will capture every unique instance of xx: and match the last instances of it. Regular Expression as you know can not moddify or alter the original string, only return a specific match, which itself can be used to specify parts of the string to moddify. ![]() So since you would like to replace all further instances aside from the first one, I'd assume you need regex to match everything but the first so you can replace them. Within these steps, you can replace the group item by something unique (e.g. However, since I'm not a fan of over-sized regexes - and for the case that you have a bigger number of potential matches - I would prefer a solution like this:Ĭombine all lines, belonging to the same group (as you already mentioned: How to merge lines that start with the same items in a text file). The result will contain a few empty lines, but I'm sure, you can solve that. The \n at the beginning of the substitution is needed for a formatting issue The ? (= optional) match won't give you an error if there aren't enough matches for all substitutions. You have to copy this regex as often as needed to match all lines (i.e. (\1: ( *))? = optional matches on consecutive lines, belonging to the same group. ^( ): ( *) = matches on the first line of a group The problem with the substitution is the uncertain number of matches. Prefix of following line only if matches current. Summary: Space after each prefix is converted to tab. Also space after colon would always be matched - regardless prefix. \1?\2? will optionally replace the same prefix if in the following line. (.*\n?) third capture group for stuff between prefixes. (?<=(\w\w:)|(\w:)) first two capture groups inside lookbehind for capturing prefix: Two or one word characters followed by a colon. Capturing inside the lookbehind is important for not consuming / not skipping a match. ![]() While Perl requires alternatives inside lookbehind to have the same length, PCRE allows alternatives of variable length.Īn idea that requires to add a pipe for each character of max prefix length: (?<=(\w\w:)|(\w:)) (.*\n?)\1?\2?Īnd replace with \t\3. ![]() PCRE is not fully Perl-compatible when it comes to lookbehind. Keeping the remainder of the rows aligned is, of course, sugar on the cake…Īs a workaround for variable length lookbehind: PCRE allows alternatives of variable length If you know better, please, point me into the right direction. On the other hand, I might just not be able to come up with the right question to search the net for. I wonder, whether there is an elegant (say: one search pattern, one replacement, run once) solution at all. How to merge lines that start with the same items in a text file Not sure, it's going into the right direction anyway, though. Is rejected for the lookbehind not being fixed length. Gets me anywhere close to what I'm looking for. Matches correctly, neither the replacement \1:\t\2\r\t\3\r There could be up to about a dozen (and a half) lines starting with the identical sequence of characters. If consecutive lines start with the same sequence of characters up to / including some separator (here the colon (and the blank following it)), only the first instance should be preserved - as should be the remainder of all lines. Cd: asdf, asdfas dfasdfa,asdfasdfa,afdsfa sdf
0 Comments
Leave a Reply. |