I want to do a global find of all instances of
TestBed.get(*)
and replace with
TestBed.inject<*>(*)
I can't seem to figure out the regex I need to just match what is between the brackets
I have tried TestBed.get(.*) and replace with TestBed.inject<$1>($1) but the ends up with an extra set of brackets
You have to escape the literal ( and )
TestBed.get\(([^)]*)\)
In TestBed.get(.*) the brackets are considered for making the groups. So you need to escape those. Try finding this instead:
TestBed.get(\(.*)\)
Also instead of a wild card pattern, you may want to use more precise patterns to match the contents inside the brackets.
In vim editor, I want to delete parentheses and the words in parentheses using regular expression.
Help me please!
As-is:
DOT("."), COMMA(","), SEMICOLON(";"), COLON(":"), QUOTE("'"),
EQUALS("="), NOT_EQUALS("<>"), LESS_THAN("<"), LESS_EQUALS("<="),
Want To-be:
DOT, COMMA, SEMICOLON, COLON, QUOTE,
EQUALS, NOT_EQUALS, LESS_THAN, LESS_EQUALS,
Here is a short one:
%s/(.\{-})//g
Explanations: it matches a parenthese (, then as few characters as possible .\{-} before the next closing parenthese ). It replaces this whole match by nothing.
To keep it simple without having a too much strict regex, I would use
:%s#("..\?")##g
This will basically remove any character or two within double quotes and parenthesis.
Is using also # instead of / it may be easy to read and in some cases helps to avoid escaping / when required.
You should really take the time to learn regex properly, it's fairly useful and pretty cool stuff. That being said, this is a good time to learn at least this part.
You have a text list and you want to match everything that isn't within parentheses, repeatedly over a line.
%s/\([^(]*\)[^)]*)\([^(]*\)/\1\2/g
First, we're gonna do this over the whole file, so let's use %s. Next, we have / as our separator. Our pattern that we'll match is therefore \([^(]*\)[^)]*)\([^(]*\).
Let's break that down some more. \( \) is the grouping operator, which just tells vim "hey, I might want the stuff in here later." [^ ] is the not operator, and says "I a character that isn't any of these characters". [^)]* then says "I want all the characters I can grab in a row that aren't ")". All of that was group one.
After our first \( \) we have stuff that isn't in a group, because we don't want to keep it. [^)]*) uses the not operator again, to match a bunch of characters that aren't ")", and then we have a ")", which matches a literal ")" (there's probably a better way to do this part, but it works.
Next, we have our second \( \) group which contains [^(]*. Again, another not operator, matching as many non "(" in a row as we can. We need our pattern to stop by the next "(" so that our regex can match multiple times on the line; if we'd used \(.*\) instead, we'd have to run our regex a bunch of times since we'd only remove one set of parens per run.
After our pattern, we have another / which delimits the pattern what we're going to put in it's place. Remember how I said \( \) tells vim to keep the stuff inside for later? Here's where we use it. Our first group is basically "everything before a (" and our second group is basically "everything after a )". We tell vim we want to just keep group 1 followed by group 2 with \1\2.
Finally, /g means do to our regex globally over the line, meaning to try matching more than once in the line if possible.
Try this pattern:
(?:[A-Z]{3,9}|, |_){1,2}
You can test it online
Many of the solutions already given are excellent. Like some of the others, I'd recommend learning how to regex in more depth. For your specific issue, you could alternatively search for opening brackets with /( then use da) to delete the brackets and their contents (skip if you want to keep this particular pair), move to the next match with n, repeat the deletion with ;, and do this until you've deleted what you need.
This seems to work:
%s/("[.,;:'=<][>=]*")//g
I'm trying to grab 2 items from a simple line.
[Title](Description)
EDIT: actually a url looking to display called it description because i want it displayed not actually parsed.
[Trivium](https://www.youtube.com/user/trivium)
Grabbing between the brackets (...) doesn't seem to work at all for me. I've googled and found several variations with no luck, Thanks in advance :)
EDIT:
Tried the following:
[(.+?)]\((.*)\)
[(.+?)]\([^\(\r\n]*\)
[(.+?)]((.+?))
and a cpl more I cant find again
The first regex you listed almost has it right. Try using this regex instead:
\[.+?\]\((.*)\)
As #PM 77-1 pointed out, you need to escape the brackets by placing a backslash in front of them. The reason for this is that brackets are special regex metacharacters, or characters which have a special meaning. Brackets tell the regex engine to look for classes of characters contained inside of it.
Your original regex [(.+?)]\((.*)\) is actually doing this:
[(.+?)] match a period '.' 1 or more times
\((.*)\) match (anything), i.e. anything contained in parentheses
So this regex would match .....(stuff) but would not match [Title](Description), the latter which is what you really want.
Here is a link where you can test out the working regex:
Regex 101
I might have a subject that looks like this AAA12345678 or BB-AAA1234
And I want to match AAA1234 either at the beginning of the subject or immediately following a dash.
I thought I could use ^|-AAA1234 but that doesn't want to work.
I'm new to regular expressions so any help would be great.
You need to put the first part of the regex inside parentheses: (^|-)AAA1234.
Given a user input string, I need to find if it end with one of the following - .com, .net , .edu., html etc.
Is there any way to do this with the regex square brackets?
I tried $[.com|.net|.html] and $[(.com)(.net)(.html)] but both don't work.
You want to use a capture group.
(\.com|\.net|\.html)
This will match either one of the values. You can add additional values by appending another pipe inside the parenthesis and placing the new value after the pipe.
You could try this:
\.com$|\.net$|\.edu$
or
(\.com|\.net|\.edu)$