Regular expression for everything between two characters - regex

I'm trying to build a regular expression which allows me to remove tags in a string. These tags always look like this: {...}.
I've tried \{.*\} so far but unfortunately this won't work if these tags occur two times. For Example: {123} Hello {asdas}. The entire line would be deleted since it starts with a { and end with a }. So, how can I avoid this behaviour?
Thanks in advance.

\{[^}]*\}
would probably do it.
An opening bracket, followed by any number of anything besides a closing bracket, followed by a closing bracket.

Related

Content in between parenthesis regex

I am quite new to regular expressions and may need some help.
I would like to get all expressions that are in between of parenthesis and end with a comma followed by a few digits.
for example asdasd alfalfa (asasdasd, 2002) asdasd fasted (asdasd) sfasadas (asdd,2333)
I already got the last part of this problem working by writing ,\s?\d+\) but I am struggling to achieve a solution for the first part of my problem.
Anyone got an idea how to get this working?
Try this:
\([^,)]+, ?\d+\)
See live demo.
An important trick here is the closing bracket ) in the character class [^,)]+, which prevents the match spanning across multiple pairs of brackets, ie matching from the opening bracket of an earlier bracketed term to the closing bracket of a later bracketed term.
If you only want the term before the comma (unclear because your example and your question text don't align on this point), convert the latter part of the regex to a look ahead:
(?<=\()[^,)]+(?=, ?\d+\))
See live demo.

Regex Match between brackets (...)

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

Regular expression to match the first appearance of a character

I'm far from a regex master, and I'm trying to match the first appearance of a semicolon for a Notepad++ search-and-replace and failing miserably at it. The best I've come up with is the following:
[^.*];
I figured this would capture the beginning of the line, all characters (if any), and then get to the semicolon. But this still ended up replacing all of the semicolons in the line. It also consumed the character before the semicolon, and I have no clue at all why that happens, so if anyone could explain that phenomenon, that would be an added bonus (but of course not essential to the actual answer).
I've got nothin'.
You need to capture the output before the semicolon in a group with parentheses, then the semicolon, then the remainder of the line. The following worked for me in Notepad++:
Find: ^([^;]*);(.*)$
Replace with: \1{whatever}\2

Vim regular expression to remove block of code for all the lines

In my code I want to remove a block of code that starts with a bracket and ends with a bracket. For example if I have this line
ENDPROGRAM { fprintf(sdfsdfsdfsd....) }
and after running the regex i want it to just end up with
ENDPROGRAM
I want to only delete code inside the bracket and the brackets themselves. I tried this command
:%s/\{[a-zA-Z0-0]*\}//g
but it says that pattern not found. Any suggestion?
ENDPROGRAM is just an example, I have like DIV MULT etc etc
Since you're using Vim, an alternative is to record a keyboard macro for this into a register, say register z.
Start recording with qz.
Search forward for ENDPROGRAM: /ENDPROGRAM[enter]
Scan forward for opening brace: f{
Delete to matching brace: d%
Finish recording q.
Now run the macro with #z, and then repeat with ##. Hold down your # key to repeat rapidly.
For one-off jobs not involving tens of thousands of changes in numerous files, this kind of interactive approach works well. You visually confirm that the right thing is done in every place. The thing is that even if you fully automate it with regexes, you will still have to look at every change to confirm that the right thing was done before committing the code.
The first mistake in your regex is that the material between braces must only be letters and digits. (I'm assuming the 0-0 is a typo for 0-9). Note that you have other things between the braces such as spaces and parentheses. You want {.*}: an open brace, followed by zero or more characters, followed by a closing brace. If it so happens that you have variants, like ENDPROGRAM { abc } { def }, this regex will eat them too. The regex matches from the first open brace to the last closing one. Note also that the regex {[^}]*} will not work if the block contains nested interior braces; it stops at the first closing brace, not the last one, and so ENDPROGRAM { { x } } will turn to ENDPROGRAM }.
The second mistake is that you are running this on all lines using the % address. You only want to run this on lines that contain ENDPROGRAM, in other words:
:g/ENDPROGRAM/s/ {.*}//
"For all lines that contain a match for ENDPROGRAM, find a space followed by some bracketed text, and replace it with nothing." Or else:
:%s/ENDPROGRAM {.*}/ENDPROGRAM/
THIS looks like a job for: (dum da dum duuuuum!)
TEXT OBJECTS!
Place the cursor anywhere within the braces. Type daB.
WOOOOOOOAAAH what just happened?!
aB is something called a "text object" in Vim. You could also have typed da{ or da} in this situation. A text object is a thing that Vim's operators can act on. d is one such operator. I'm sure you know others: c, y, etc.
Visual mode also works on text objects. Instead of daB, try vaB. This will select all the text in the braces, plus the braces themselves. Most text objects also have an "inner" variant, for example ciB would delete everything inside the braces, and enter insert mode, leaving the braces intact.
There are text objects to work with HTML/XML tags, objects for working with quoted strings, objects for sentences and paragraphs, objects for words and WORDS, and more. See the full list at :help text-objects.
When something is broken, start simple and work up to what you need. Do not worry about the :s command at first; instead, focus on getting the pattern (or regular expression) right.
Search for \{ and you will get an error message. Oops, that should be just {.
Add the character class: {[a-zA-Z0-0]*. Darn, that is not right, because you left out the space.
Next try: {[a-zA-Z0-0 ]*. Now we are getting somewhere, but we also want to match the parentheses and the dots: {[a-zA-Z0-0 ().]*.
Add the closing brace, realize that you really meant 0-9 instead of 0-0, and you are done: {[a-zA-Z0-9 ().]*}.
At this point, you can take advantage of the fact that :s uses the current search pattern by default, so all you need is :%s///.

Regular Exp. in Eclipse, Find and Replace: Match everything between curly braces

I would like to match everything between and including curly braces in eclipse find and replace (it can be assumed that there are no inner curly braces but any other character including all types of whitespace.
int SomeMethodName() {
// TODO Auto-generated method stub
return asdfasdf.rearoiula12123893;
}
Right now I am trying this and it only matches curly braces with nothing in them \{[.\s]*\}
The . inside a character class means a . literal, not a wildcard. You need something more like:
\{.*?\}
Depending on how Eclipse treats new line characters you might need to change it to:
\{(.|\r\n?|\n)*?\}
This should work. Tested using Regex Powertoy here.
\{[\s\W\w]*\}
EDIT:
\{[\s\w\. /=(":);]*\} should stop at the nearest closing brace. The piece after the space has all the miscellaneous non-word characters, so you might have to add to that depending on the nature of what you're parsing (e.x. a weird String).