regex preserve whitespace in replace - regex

Using REGEX (in PowerShell) I would like to find a pattern in a text file that is over two lines and replace it with new text and preserve the whitespace. Example text:
ObjectType=Page
ObjectID=70000
My match string is
RunObjectType=Page;\s+RunObjectID=70000
The result I want is
ObjectType=Page
ObjectID=88888
The problem is my replacement string
RunObjectType=Page;`n+RunObjectID=88888
returns
ObjectType=Page
ObjectID=88888
And I need it to keep the original spacing. To complicate matters the amount of spacing may change.
Suggestions?

Leverage a capturing group and a backreference to that group in the replacement pattern:
$s -replace 'RunObjectType=Page;(\s+)RunObjectID=70000', 'RunObjectType=Page;$1RunObjectID=88888'
See the regex demo
With the (\s+), you capture all the whitespaces into the Group 1 buffer and then, using $1 backreference, the value is inserted into the result.

Related

How to encase words with quotations?

I am currently trying to convert a list of 1000 words into this format:
'known', 'buss', 'hello',
and so on.
The list i have is currently in this format:
known
worry
claim
tenuous
porter
I am trying to use notepad++ to do this, if anybody could point me in the correct direction, that would be great!
Use this if you want a comma delimited list but no extra comma at the end.
Ctrl+H
Find what: (\S+)(\s+)?
Replace with: '$1'(?2,:)
CHECK Wrap around
CHECK Regular expression
Replace all
Explanation:
(\S+) # group 1, 1 or more non spaces
(\s+)? # group 2, 1 or more spaces, optional
Replacement:
'$1' # content of group 1 enclosed in quotes
(?2,:) # if group 2 exists, add a comma, else, do nothing
Screen capture (before):
Screen capture (after):
How about replacing (\S+) with '$1'? Make sure your Regular Expression button is selected in the Find and Replace tool inside Notepad++
Explanation
(\S+) is regex for repeating non-whitespace characters (1 or more). Wrapping it in parenthesis puts it in a capture group which can be accessed in numerical order by using a dollar sign ($1).
'$1' will take that found text from the Find above and replace it with capture group #1 ($1) wrapped in single quotes '.
Sample
Input: known worry claim tenuous porter
Output: 'known' 'worry' 'claim' 'tenuous' 'porter'

Regex to select and replace stuff, keeping patterns

I want to change some strings:
space+cows --> space + cows
stupid+rabbit --> stupid + rabbit
(put spaces around the `+`)
In Sublime Text 2, I tried to use these:
Find: \w+\+\w+
Replace: \w+ \+ \w+
The finding regex matched everything well, but obviously, my strings were replaced with literally
w+ + w+.
One more example:
Strings:
bool *foo --> bool* foo
int *bar --> int* bar
Pattern:
Find: (bool|int) *(foo|bar)
Replace: (bool|int)* (foo|bar)
Result:
(bool|int)* (foo|bar)
(bool|int)* (foo|bar)
Needless to say I wanted to keep the actual bool, int, foo and bar as they were before.
I also cannot use only ­ \* to match the strings because it would select other stuff that I don't want to replace; I need some context around the actual ­ \* to select the correct strings. In the same way, I cannot use patterns like ­ \*[^ ­ ] because the not-space character after the asterisk would be obliterated after replacement.
I fixed my problem by using Sublime Text's multiline edition but I am still wondering: is it possible to use a regex in such a way that you can replace strings containing "group of characters" without wiping the actual contents of the "group of characters"?
Yes, this is possible. The reason your replacements don't work is (as you've noticed) your replacement text is just literal text; whatever you put in the box is what replaces what was matched as you would expext.
What you need to do is use a RegEx capture for this. What this does is make the regular expression processor (in this case Sublime Text) not only match the test but also store it for use in the replacement. You do that by wrapping the parts of the match you want to save in parenthesis. Each set of parenthesis is a Capture Group.
For your example, your regex becomes"
(\w+)\+(\w+)
The value of the match inside each set of parenthesis is saved into it's own numeric group, starting at one. A syntax like the following expands out to the contents of the first match, followed by the plus sign with spaces around it, followed by the second word:
\1 + \2
You can use each number multiple times, if you want:
\1 and again \1 and also \2
Regex to turn "stupid+rabbit" to "stupid + rabbit"
Find: (\w+)\+(\w+)
Replace: $1 + $2
Regex to turn "bool *foo" or "int *bar" into "bool* foo" or "int* bar"
Find: (bool|int) \*(foo|bar)
Replace: $1* $2
() - forms groups which can be later used. $1 is the first group and $2 is the second group.

Find and replace parts of matched string in Notepad++

I have something in a text file that looks like '%r'%XXXX, where the XXXX represents some name at the end. Examples include '%r'%addR or '%r'%removeA. I can match these patterns using regex '%r'%\w+. What I would like to replace this with is '{!r}'.format(XXXX). Note that the name has to stay the same in the replace so I'd get '{!r}.format(addR) or '{!r}.format(removeA). Is there a way to replace parts of the matched string in this way while retaining the unknown variable name pulled out with \w+ in the regex search?
I'm specifically looking for a solution using the find and replace features in Notepad++.
You can use
'%r'%(\w+)
and replace with '{!r}.format\(\1\)
The '%r'%(\w+) pattern contains a pair of unescaped parentheses that create a capturing group. Inside the replacement pattern, we use a \1 backreference to restore that value.
NOTE: The ( and ) in the replacement must be escaped because otherwise they are treated as Boost conditional replacement pattern functional characters.
See more on capturing groups and backreferences.
Search on:
'%r'%(XXXX)
Replace with:
Whatever You like \1
\1 will match the first set of grouping parentheses.

Replace regular expression placeholder followed by number in Sublime Text 2

Let's say I want to add a 0 after every word
(\w+)
The following replacement string doesn't work.
$10
So, how do I convert
this is my string
into
this0 is0 my0 string0
Use braces around the group ID in the replacement string:
${1}0
The braces tell the regex engine that the number inside them is the actual Group ID. The 0 that will follow will be treated as a literal zero.
BTW, you can also get the same result with \w+ regex and ${0}0 replacement string, no need in capturing groups.
Or, using \n syntax, it works like this:
Find: (\w+)
Replace: \10

Notepad++ regex get 2nd group

I have a tex with 32k lines like this example
A01.2 Some Text
A01.3 some Text
A01.4 3Some Text
A02.0 [some text]
B02.1 Text .05 example
I need to replace white spaces with ';' symbol.
I tried (\S{3}\.\d)(\s) but notepad++ highlights/gets both groupsB02.1 with whitespace.
1st question: how do i disable 1st group, or take only 2nd
2nd question: is there another expression do find only this white space?
Here is the real example:
If you want to replace the whitespace by ;, so this B02.1 will be B02.1; using notepad++; since you're capturing the groups then use $ notation in the replace expression.
Find: (\S{3}\.\d)(\s)
Replace: $1;
$1 is for the first captured group.
Hope it helps,
You disable the first group simply not grouping it:
\S{3}\.\d(\s)
Otherwise, the look-behind may suite your case:
(?<=\S{3}\.\d)(\s)
Use a lookbehind so B02.1 won't get matched:
(?<=\S{3}\.\d)(\s)