Replacing with backreference in TextMate issue - regex

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 \.

Related

Regex: How to get all words, special characters and white spaces between quotation marks?

Currently I have a regex expression ([^\[\][\[^\[\][\n"]+) to match text between "", but this does not capture whitespaces, for e.g. if I enter " hello ", it will return hello, without the spaces before and after the word.
Is there some expression I can use to just simply catch anything between two quotation marks?
Thank you.
Maybe this will help:
(?<!\\)(\"|')(.+?)(?:(?<!\\)\1)
And to get the text inside the quotes, get the second capture group.
Proof.
Explanation
(?<!\\) - Negative lookbehind. Looks for literal backslash ('')
(\"|') - to test for the start of the "string"
(.+?) - . will match anything but newlines.
+? means as much as possible but only as much needed to match.
(?:(?<!\\)\1) - Non capturing group.
Used here so we can use the (?<!\\) described earlier without looking behind the whole expression. The
\1 matches the first capture group ((\"|')). Can be replaced with $1
You should use following regex:
\"\s*([^\"]+?)\s*\"
([^\"]+?)The text you want to get will be between space and quote.
Demo & Explanation

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 to capture string inside parenthesis

I have tried
[^\(]\(.*\)
but in a string like
Tamara(PER) Jorquiera
The pattern returns
a(PER)
How can I get only the text inside parenthesis, assuming the open and close parenthesis occur once?
[^\(]\(.*\)
It matches any character that is not ( (a in the example).. and a literal ( then everything till )
You can use the following:
\([^)]*\) // this will match only the text between parentheses
// including parantheses
If you want only the text use lookahead and lookbehinds:
(?<=\()[^)]*(?=\))
See DEMO
Use following regex :
\(([^)]*?)\)
Demo
Note that you don't need [^\(] out side of the parenthesis just use
[^)]*? within a capture group.
[^)]*? is a non-greedy regex that match anything except closing parenthesis.

Replacing a string in Sublime

I'd like to replace this
y[100] with this Ith(y,100) in Sublime3.
I've got the regular expression \by[\d+] in find what and Ith(y,$1) in replace with, but it doesn't work. It finds what to replace correctly but just replaces it with Ith(y, )
You need to pick the \d+ inside () to capture in group($1)
\by[(\d+)]
Also, you may need to to escape the [ and ] characters here.
\by\[(\d+)\]
You have to capture the data you want to backreference: to do that you have to use unescaped parenthesis (...), ie capturing groups. Also, [...] are character classes, a special character in regex that you need to escape.
Try replacing
\by\[(\d+)\]
with
Ith(y, \1)
You need to escape the special characters and capture the number in a capturing group group. Try this regex:
\by\[(\d+)]\b
And replace with
Ith(y, $1)
Online explanation and demonstration: http://regex101.com/r/nX3yJ9