Regex - find string and between parenthesis values
Another regex quandary for me… another find and replace.
How would I say to find a string and return only what is in the parenthesis after the string.
So only leave what is between the parenthesis, and delete everything else.
Why not try the solution HERE this time around?
It is fairy easy to match text between two different symbols.
Above you have
The only trick is that with regular-expressions, those characters mean something to the search beyond their literal values.
So you have to “escape” them, like
\is known sometimes as the escape character, removing special meaning from the character directly following.
Thus I think
\(.+?\)could be what you need to “mark” your text for copying (referring to the technique in the other thread for what comes after the marking).
Or, to mark it without including the parens, this might work better:
Thanks Alan, I guess I should have posted a bit more.
There are other variables that also have same parenthesis pattern, i.e.
I dont need that because it is duplicative of
I only need what is in the parenthesis of this variable.
So I was trying to say, only return what is after this variable, and only what is in the first set of parents following that variable, and both of these are on the same line, along with 2 more variables with the same pattern.
The concepts involved seem the same as your previous question: the exact details are slightly different, but it’s still “match a bunch, but only keep part of the match”, which will still be accomplished by using parenthesized groups in the match, and
$1in the replacement.
So why don’t you give it a go: come up with a few lines of example text, some lines which should match and get transformed, and some which should not; then experiment with a regex that you think should work, based on the regexes from the previous discussion. If it works: great, you’ve learned how to do it yourself! If it doesn’t work: great, a learning opportunity!
If it doesn’t work, come back to here:
Post your example “before” data. Use the
</>button on the forum editor (paste your text, select your text, hit
</>) so that the text doesn’t get edited by the forum.
In another section (separate
</>), post your example “wanted/after” data – you will have to manually create this.
In another section, show the exact FIND and REPLACE you tried.
After that, try to explain why you thought it would work.
If you don’t go through this exercise, you won’t ever learn how to do it yourself. And we are quickly going to stop answering you, if you don’t show a willingness to learn and try.
Suggested post format, for use by @Anthony-Noriega :
Here is my “before” data:
before data here
Here is my desired “after” data:
after data here
Here is what I actually got
messed up after data here
Here is what I tried
find: (?-s)/\.\*\K(.+?)(?=\.\*/) replace: ?1\1\r\n
Here is why I tried it: it’s a direct quote from the previous thread, without modification; I am not sure why I thought it would work
Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as plain text using the
</>toolbar button or manual Markdown syntax. Screenshots can be pasted from the clipboard to your post using
Ctrl+Vto show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get… Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.
Yea, I second what Peter says.
It seems like whatever Anthony is currently working on, he’s going to have a lot more of these situations.
Pays off big time to LIYSYCDIY (Learn It Yourself So You Can Do It Yourself).
@Alan-Kilborn I’m trying. Regex ain’t easy to lean. Ill take these concepts and start putting them to practice.
Regex ain’t easy to learn
But as you’ll see, you can’t use the Community for more than a couple of times for purely free data transformations before people start getting “edgy” about it. :-)
@Alan-Kilborn Fair enough.
So in what I gave you last, I used this construct leading it off:
This means that before the text you want, a
(must appear, and note that the
(isn’t wanted as part of the matching text.
So if you want to reach further back, just insert stuff between the
\, so, say, like this:
Often with regex, this kind of “command” is used:
...is different things at different times.
(?#...)is a comment
(?<=...)is a lookbehind assertion for equality (ok, assertion is an advanced concept at this point)
The key point is to note the
(?at the start, and the
)at the end, then mentally disregard and see what is left, which in the latter case, is
<=which is an arrow pointing backward, and an equal sign, so you should think “text that comes earlier must match what comes after the equal sign”.
Just some tidbits.
I’m trying. Regex ain’t easy to learn
Just as a suggestion. Break down your problem into smaller steps. You are looking for some text on a line, and then that line needs to return something else on the line. So we have 2 steps. The first is to identify a particular line. So that would suggest using the “bookmark” function. You could easily do that, then perhaps copy bookmarked lines elsewhere (another tab). Then you could erase all characters up until the first “(”. Then erase everything after the “)” bracket. These 3 steps are all very easy to achieve with regex. Indeed, they are in reality some of the first steps you would take in learning regex.
If you come to us you will likely get a 1 step complete solution, and unfortunately you will find that difficult to understand and that will likely not be helpful in you learning regex.
I always say that you have to get a good foundation in regex before attempting complicated problems, and that may also involve breaking down the problem into smaller steps so that each can possibly become a simple problem.
And while learning, if you do have a problem that you can’t overcome we WILL help if you can provide evidence of what you have tried, even if it failed. At least we can see some intent on your behalf.