Replace regular expression placeholder followed by number in Sublime Text 2 - regex

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

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'

What is the syntax in TextMate to insert a new line '\n' before every capital letter? [duplicate]

I am using TextMate to replace expression [my_expression] consisting in characters between open and closed brackets by {my_expression}; so I tried to replace
\[[^]]*\]
by
{$1}
The regex matches the correct expression, but the replacement gives {$1}, so that the variable is not recognised. Can someone has an idea ?
You forgot to escape a character, [^]] should be [^\]].
You also need a capture group. $1 is back-referencing the 1st Capture Group, and you had no capture groups, so use the following Regex:
\[([^\]]*)\]
This adds () around [^\]]*, so the data inside the [] is captured. For more info, see this page on Capture Groups
However, this RegEx is shorter:
\[(.*?)\]
Also substituting with {$1}
Live Demo on Regex101
Use a capturing group (...):
\[([^\]]*)\]
The $1 is a backreference to the text enclosed with [...].
Here is the regex demo and also Numbered Backreferences.
Also, the TextMate docs:
1. Syntax elements
(...) group
20.4.1 Captures
To reference a capture, use $n where n is the capture register number. Using $0 means the entire match.
And also:
If you want to use [, -, ] as a normal character in a character class, you should escape these characters by \.

regex preserve whitespace in replace

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.

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.

Powershell regex

Is there a Powershell regex command I could use to replace the last consecutive zero in a text string with a "M". For Example:
$Pattern = #("000123456", "012345678", "000000001", "000120000")
Final result:
00M123456
M12345678
0000000M1
00M120000
Thanks.
Search for the following regex:
"^(0*)0"
The regex searches for a consecutive string of 0 at the beginning ^ of the string. It captures all the 0 except the one for replacement. "^0(0*)" also works, since we only need to take note of the number of 0 which we don't touch.
With the replacement string:
'$1M'
Note that $1 is denotes the text captured by the first capturing group, which is (0*) in the regex.
Example by #SegFault:
"000120000" -replace "^(0*)0", '$1M'