Find/Replace - keeping a part of the find, remove everything else
-
To sum it up take this example im trying to reproduce:
This is the contents of document.txt:
For the documents i have created, there are hundreds of patterns like these, and i want to dwindle them down to:
$\vec{Q}=m \cdot \vec{\Delta v}$To do that, i would have to locate all structures just like this one inside the document, which i did with:
\!\[+[^]]+[A-Za-z{}=\/!_-\s]]\(+[^)]+[A-Za-z{}=\/()!_-\s])The problem right now is that i have no idea how to a keep a part of the find, or if its even possible. Until now all the replaces i did were only literal, replacing a find with a select string. Is there a way to separate a part of the found characters as a variable and keep them on replacement?
-
You want “capture groups” and substitutions using them.
See the user manual HERE. -
@alan-kilborn I took a look at it, added a few tweaks (by that i mean just two parantheses) and now it works perfectly. Thanks alot.
For those who might be curious to how the solution looks like:\!\[+([^]]+[A-Za-z{}=\/!_-\s])]\(+[^)]+[A-Za-z{}=\/()!_-\s]edit: and for the replace:
\$$1\$ -
Hello, @gabriel-mourão, @alan-kilborn and All,
I’m sorry, Gabriel, but your final search regex seems invalid -:((
May be it’s some typos, introduced by the forum syntax, but one correct syntax is :
!\\[([^]]+[A-Za-z{}=\\/!_-\s])\\]\([^)]+[A-Za-z{}=\\/()!_-\s]Using the free-spacing mode,
(?x), this syntax can be split as below :(?x) !\\[ ( [^]]+ [A-Za-z{}=\\/!_-\s] ) \\] \( [^)]+ [A-Za-z{}=\\/)!_-\s]Note that :
-
On one hand :
-
The part
[^]]+matches the chars range![\vec{Q}=m \cdot \vec{\Delta v} -
The part
[A-Za-z{}=\\/!_-\s]matches the single char]
-
-
On the other hand :
-
The part
[^)]+matches the chars range(vec{Q}=m_!cdot_!vec{!Delta_v} -
The part
[A-Za-z{}=\\/)!_-\s]matches the single char), at the end
-
I suppose that it’s not exactly what you want !
Your need could rather be expressed as :
-
First search for an exclamation mark and an opening square bracket
![ -
Then, search for the smallest range of chars…
-
Till an ending square bracket
] -
Now, search for an opening parenthese
( -
Then, search for the smallest range of chars…
-
Till an ending parenthese
)
This leads to this simple regex S/R :
SEARCH
(?-s)!\\[(.+?)\\]\(.+?\)REPLACE
\$$1\$Best Regards,
guy038
-
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login