Find and replace with regex in Sublime Text 2 - regex

I am using Sublime Text 2 and I am wanting to shift my bootstrap2 project to bootstrap3.
How can I replace:
span2
to
col-lg-2
My pattern is
span[0-9]+

Your regular expression is correct, just place a capturing group around the part you want captured and then reference back to that group in your replacement.
Use Ctrl + H to open the Search and Replace, enable Regular Expression..
Find: span([0-9]+)
Replace: col-lg-$1
Edit
To add a class to your input field, you can use the following:
Find: <input[^>]*class="\K[^"]*
Replace: form-control
Sublime Text 2 allows you to use the \K escape sequence. \K resets the starting point of the reported match and any previously consumed characters are no longer included.

find what: span(\d)
replace with: col-lg-$1
(\d) matches one digit and stores it in match-group 1
$1 inserts the match-group 1
do not forget to enable the regular expression button (".*").
your "span([0-9])" is valid as well

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'

Replace ) in Notepad++

How do I replace ) when it comes after 1, 2, or 3 digits (not chars, and without removing the digit(s) themselves)?
Find what: ((?<!\d)\d{1,3})\)
Replace with: $1
This ensures that the ) comes after 1 to 3 digits (no more, no less).
Just append your replacement text to the end of $1. For example, if you want to replace it with the word TEST, your replacement would be $1TEST
As simple as:
Find: ((^|[^\d])\d{1,3})\)
Replace \1
And don't forget to enable te regular expressions in the panel.
Visit this link to try a working demo.

Notepad++ Search and Replace: delete all after "/" in each row

I have a plain text file with content like this:
prežrať/RN
prežrieť/Z
prežrúc/zZ
prežuť/c
...
Q: How can I remove all strings after / symbol in every row in Notepad++?
Desired output:
prežrať
prežrieť
prežrúc
prežuť
...
I am doing this with Find-and-Replace for every different string after /, but there are too many combinations.
Search for: /.*, replace with nothing.
The character / matches just /. ., however, matches any character except newlines, so .* will match a sequence of characters up until the first newline. You can find a demonstration here: http://regex101.com/r/kT0uE3.
If you want to remove characters only after the last on the line /, you should use the regex /[^/]*$. You can find an explanation and demonstration here: https://regex101.com/r/sZ6kP7/74.
In regular expression mode
Find:
/.*
Replace:
(empty)
Set find and replace to regular expression mode.
Find string: /.*
Replace String: (empty string)
Notepad++ find and replace is by default line ended (it won't go over multiple lines)
Using find and replace:
Hit CTRL-H to open the Replace dialogue box
enter /.* into "Find what"
leave "Replace with" empty
Select "Regular expression" (and .matches newline if it is single line)
Click on Replace
Here we go... You are done.

NOTEPAD++ REGEX How do I replace and remove a whitespace from $0

I'm using this regex:
\s[0-9]+ thd
It finds what I want perfectly. I want to remove the white space at the beginning. What should I put in the replace field?
Change the search text to \s([0-9]+ thd) and then the replacement id \1 or $1 depending on the type of regex.
Find:
\s([0-9]+ thd)
Replace:
$1
So if you using Notepad++ everything in parenthesis refers to section build from \ and number in Replace box.
Look at example:
Find: (\s)([0-9]+ thd)
Replace: \1\2
This give you back first \1 and second \2 section - nothing unusual, but when you live only \2 after replace you'll get only part found by ([0-9]+ thd)
Going further if you split your expression to (\s)([0-9]+)(\s)(thd) then you'll get 4 parts
This parts will be represented by \1\2\3\4 and if you need add something more to your output line:
for example added_text_before\1added_text_after in Replace will result merging added text and section found - practice because there is no hidden magic out there.
Remember that using regex in Notepad++ have some limitations (to long expression aren't read).

While replacing using regex, How to keep a part of matched string?

I have
12.hello.mp3
21.true.mp3
35.good.mp3
.
.
.
so on as file names in listed in a text file.
I need to replace only those dots(.) infront of numbers with a space.(e.g. 12.hello.mp3 => 12 hello.mp3).
If I have regex as "[0-9].", it replaces number also.
Please help me.
Replace
^(\d+)\.(.*mp3)$
with
\1 \2
Also, in recent versions of notepad++, it will also accept the following, which is also accepted by other IDEs/editors (eg. JetBrains products like Intellij IDEA):
$1 $2
This assumes that the notepad++ regex matching engine supports groups. What the regex basically means is: match the digits in front of the first dot as group 1 and everything after it as group 2 (but only if it ends with mp3)
I tested with vscode. You must use groups with parentheses (group of regex)
Practical example
start with sample data
1 a text
2 another text
3 yet more text
Do the Regex to find/Search the numerical digits and spaces. The group here will be the digits as it is surrounded in parenthesis
(\d)\s
Run a replace regex ops. Replace spaces for a dash but keep the numbers or digits in each line
$1-
Outputs
1-a text
2-another text
3-yet more text
Using the basic pattern, well described in the accepted answer here is an example to add the class="odd" and class="even" to every <tr> element in Notepad++ or any other regex compatible editor:
Find what: (<tr><td>)(.*?\r\n)(<tr><td>)(.*?\r\n)
Replace with: <tr class="odd"><td>\2<tr class="even"><td>\4