Reputation: 13
Okay, so its like this:
I'm writing a huge programming file, and, while near the end, I realize that I made a stupid mistake. Basically, the text looks like this now:
"usable_by"
{
//Here is a line(s) that looks like the following:
"one" "1"
"two" "1"
//It can have any combination of these lines, with "one" or "two" in the lines above going up to nine.
}
Comments are obviously not part of the program, and are notes for you to help me by.
I want to replace these instances with all nine attributes, so it would be:
{
"one" "1"
...
"nine" "1"
}
With ... being 2-8.
I'm guessing you would have to devise a way to replace anything in between { and } (but only on the "usable_by" tag so that other tags would not be disrupted) with something else.
Any help is appreciated. Also, I'd like it if you were able to explain it as opposed to just giving me whatever this regex line may be.
Thanks!
Upvotes: 1
Views: 67
Reputation: 26413
Please, pretty please, backup what you've got before running this. It worked for me, but I've only got a bit of sample text.
("usable_by"\s*\{\s*\n)(\s*)[^}]*\n(\s*)
\1\2"one" "1"\n\2"two" "1"\n\2"three" "1"\n\2"four" "1"\n\2"five" "1"\n\2"six" "1"\n\2"seven" "1"\n\2"eight" "1"\n\2"nine" "1"\n\3
( # Start of first capturing group. Save for use in the replacement.
"usable_by" # This should be obvious.
\s* # \s match any whitespace character, the * means "zero or more."
\{ # The opening curly brace.
\s* # More optional additional space.
\n # Newline.
) # End of first capturing group.
(\s*) # Second capturing group. Grabs the attribute indentation.
[^}]* # Everything up to the closing curly brace.
# Except it'll backtrack a bit so we can get rid of the next two.
\n # Newline, the last before your final } line.
(\s*) # Third capturing group. Picks up the indentation
# leading up to the closing curly brace.
\1 # Back-reference to the first capturing group. Puts it here.
\2 # And the second, your indentation. We'll reuse this a bunch.
"one" "1" # Your first attribute.
\n # A newline
\2 # There's your indentation again.
"two" "1" # And second attribute.
\n # And newline.
... # etc. don't really need to explain them all individually.
\2"nine" "1"\n # Indentation, ninth attribute, newline.
# You're used to all this by now.
\3 # The third capturing group. The indentation leading
# up to your closing }.
Upvotes: 1