In my work I have a very repetitive task, and I would believe that regex is a fast solution for it, though, my knowledge of regex is very sparse.
What I want to do is to in a txt-file find all occurences of
$text$
and replace them with
\\( text \\)
where text is LaTeX-code, so it can contain alphanumeric as well as other characters.
I plan to do this in Sublime Text, as it has a built in and convenient regex engine. Can anyone help?
Regex:
\$([^$]*)\$
REplace With:
\\\\( \1 \\\\)
Related
In a long corpus of text, I want to make some corrections in certain
environments. However, I am encountering problems when using regex with text
editors. I switched to gedit to have an editor which supports regex in
search & replace.
Crucially, I only want to make changes if the line starts with a certain
pattern (\nm or \mb). The problem is that the element that I want to
replace (o' -> o'o) is not at a fixed length from the beginning of the line
and I can't include the regex in the lookbehind (the lookbehind fails).
Is there any way to include what I am looking for in a simple text editor
regex? Or is this already a step where I have to learn how to script in, for
example, Python?
This is what the regex looks like so far.
(?<=\\(nm|mb)).*o'(?=(q|w|r|t|z|p|s|d|f|g|h|j|k|l|x|c|v|b|n|m|a|i|u|e))
Of course, I can't apply .* in the replace without losing its content.
Put a capture group around .* and a back-reference in the replacement.
Find: (?<=\\(nm|mb))(.*)o'(?=(q|w|r|t|z|p|s|d|f|g|h|j|k|l|x|c|v|b|n|m|a|i|u|e))
Replace: \1o'o
I am a newbie in regex. I want to write a regex for string replacement which can be used in NotePad++ or any other editors which support regex. I want to replace the one in bold with nothing. Please see the below example.
0d12345678901234p+123456789012.0AA
12345678901234.0AA
Is this the only possible pattern in your entire file? if so you can use ^(0d)|p\+[0-9]* pattern to search and batch replace it with nothing.
I have a text like this
English text||Arabic text||Japanese text||Arabic text||numbers
I tried using (\|\|\p{Han}\p{Hiragana}\p{Katakana}\|\|) but I'm getting "invalid Regular Expression" error message in notepad++, although it's right as I tested it in This regex tester, Plus this will only look at the Japanese text with Katakana after Hiragana after Kanji, how can I make it look an the Japanese text without that order?
Notepad++ does not support \p modifier, try \p{Letter} (should match any letter in any language) and you will see no match.
You can use some other application, e.g. a very good one is EditPad.
I figured what's the main problem, I have to [\p{Han}\p{Hiragana}\p{Katakana}] if I don't want it in a specific order, so all what I had to do is find: \|\|([\p{Han}\p{Hiragana}\p{Katakana}]*?)\|\| and replace it with ||.
Of course Notepad++ didn't work, So I used EditPad as NikitOn suggested
In sublime text, I need to replace all instances of:
rs('???????')
with
$rs['??????']
(keeping the ??????? part the same). I'm a regex virgin and I've only got as far as locating the regex button on the find and replace panel.
How is this possible.
Find regex:
rs\('(.*?)'\)
Replace:
$rs['$1']
Beware that this will expect anything inside the quotes; if it isn't what you need, please tell me :)
I got a string like this:
PREFIX-('STRING WITH SPACES TO REPLACE')
and i need this:
PREFIX-('STRING_WITH_SPACES_TO_REPLACE')
I'm using Notepad++ for the Regex Search and Replace, but i'm shure every other Editor capable of regex replacements can do it to.
I'm using:
PREFIX-\('(.*)(\s)(.*)'\)
for search and
PREFIX-('\1_\3')
for replace
but that replaces only one space from the string.
The regex search feature in Notepad++ is very, very weak. The only way I can see to do this in NPP is to manually select the part of the text you want to work on, then do a standard find/replace with the In selection box checked.
Alternatively, you can run the document through an external script, or you can get a better editor. EditPad Pro has the best regex support I've ever seen in an editor. It's not free, but it's worth paying for. In EPP all I had to do was this:
search: ((?:PREFIX-\('|\G)[^\s']+)\s+
replace: $1_
EDIT: \G matches the position where the previous match ended, or the beginning of the input if there was no previous match. In other words, the first time you apply the regex, \G acts like \A. You can prevent that by adding a negative lookahead, like so:
((?:PREFIX-\('|(?!\A)\G)[^\s']+)\s+
If you want to prevent a match at the very beginning of the text no matter what it starts with, you can move the lookahead outside the group:
(?!\A)((?:PREFIX-\('|\G)[^\s']+)\s+
And, just in case you were wondering, a lookbehind will work just as well as a lookahead:
((?:PREFIX-\('|(?<!\A)\G)[^\s']+)\s+
You have to keep matching from the beggining of the string untill you can match no more.
find /(PREFIX-\('[^\s']*)\s([^']*'\))/
replace $1_$2
like: while (/(PREFIX-\('[^\s']*)\s([^']*'\))/$1_$2/) {}
How about using Replace all for about 20 times? Or until you're sure no string contains more spaces
Due to nature of regex, it's not possible to do this in one step by normal regular expression.
But if I be in your place, I do such replaces in several steps:
find such patterns and mark them with special character
(Like replacing STRING WITH SPACES TO REPLACE with #STRING WITH SPACES TO REPLACE#
Replace #([^#\s]*)\s to #\1_ server times.
Remove markers!
I studied a little the regex tool in Notepad++ because I didn't know their possibilities.
I conclude that they aren't powerful enough to do what you want.
Your are obliged to learn and use a programming language having a real regex capability. There are a number of them. Personnaly, I use Python. It would take 1 mn to do what you want with it
You'd have to run the replace several times for each space but this regex will work
/(?<=PREFIX-\(')([^\s]+)\s+/g
Replace with
\1_ or $1_
See it working at http://refiddle.com/10z