Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more. How to remove square brackets in string using regex?
Ask Question. Asked 10 years ago. Active 5 years ago. Viewed 96k times. Gumbo k gold badges silver badges bronze badges.
Active Oldest Votes. Mark Byers Mark Byers k gold badges silver badges bronze badges. Zypps [ Consequently, if you want to match [ and ] characters, you need to escape them so that they're interpreted as simple characters.
The first bracket within the character class doesn't have to be escaped. Fourier Fourier 2 2 silver badges 6 6 bronze badges. You probably don't even need string substitution for that. Greg Hewgill Greg Hewgill k gold badges silver badges bronze badges. See: json. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.
Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm trying to use a regular expression in C to match a software version number that can contain:.
I can get to the point where the version number is validating correctly, but it accepts anything that comes after for example: " This happens because the regex matches part of the string, and you haven't told it to force the entire string to match.
Also, you can simplify your regex a lot for example, you don't need all those capturing groups :. The error message you mentioned has to do with the fact that you need to escape the backslash, too, if you don't use a verbatim string.
The latter form is preferred. Learn more. C regular expression to match square brackets Ask Question. Asked 7 years, 3 months ago. Active 3 years, 5 months ago. Viewed 16k times. I'm trying to use a regular expression in C to match a software version number that can contain: a 2 digit number a 1 or 2 digit number not starting in 0 another 1 or 2 digit number not starting in 0 a 1, 2, 3, 4 or 5 digit number not starting in 0 an option letter at the end enclosed in square brackets.
Some examples: AlG TimCross TimCross 71 3 3 gold badges 3 3 silver badges 7 7 bronze badges. Is this matching the entire string or is this version number in the middle of a sentence? Active Oldest Votes. Tim Pietzcker Tim Pietzcker k 53 53 gold badges silver badges bronze badges.
Bob Vale Bob Vale Careful, in. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
The Overflow Blog. Podcast Ben answers his first question on Stack Overflow. The Overflow Bugs vs. Featured on Meta. Responding to the Lavender Letter and commitments moving forward. Visit chat.
Is there an easier way to do this ie with regexso that I do need to loop through every character? Here's another approach, an ugly hack which turns the input into a JS array representation and then parses it using JSON. So you mean that if you were to have a string of characters Xyou would like to check if the string combination Y is contained within X? The above would then return the string literal Y as it matches what is inside str.
Active 4 years, 6 months ago. Viewed 1k times. Braiam 4, 11 11 gold badges 45 45 silver badges 68 68 bronze badges. Matthew S.
Mathletics No, that is not needed. Mar 21 '15 at You're parsing a little language, with nesting.
Character Class Intersection
There is nothing hacky whatsoever about iterating over the characters in the input--that's what parsers do. What would be hacky is a hard-to-read, hard-to-maintain, limited regexp such as was suggested in one answer. I just thought that if there was an easier and clean way with regex, I could use that instead. Is this an array? What is this supposed to be? Active Oldest Votes. Not sure if I get the question right sorry about that. Alhamza Alnaimi Alhamza Alnaimi 97 1 1 gold badge 2 2 silver badges 10 10 bronze badges.
Sign up or log in Sign up using Google.It makes it easy to match any single character that must be present in two sets of characters. You can use the full character class syntax within the intersected character class. In other words: it matches a single consonant. Without character class subtraction or intersection, the only way to do this would be to list all consonants: [ b - d f - h j - n p - t v - z ]. You can intersect the same class more than once. The nested square brackets are only needed if one of the parts of the intersection is negated.
If you do not use square brackets around the right hand part of the intersection, then there is no confusion that the entire remainder of the character class is the right hand part of the intersection. PowerGREP does not allow anything after the nested ].
This way there is no ambiguity about their meaning. PowerGREP treats them as literal ampersands. In Ruby, intersection takes precedence over negation. If you want to negate the right hand side of the intersection, then you must use square brackets. Those automatically control precedence. Thus this regex is the same as [ 12 ]. The ampersand has no special meaning in character classes in any other regular expression flavors discussed in this tutorial.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up. Now I want to replace the pair of brackets by a pair of square brackets to make this line become aaa bbb [ccc] ddd.
In words you might describe the substitute command as: look for a " " followed by anything followed by " ". Replace that by "[" followed by the same "anything" followed by "]". Here we first define a dictionary with the keys of what you want to have replaced. So we need a key with the value [ since that is the replacement char you want. Also we want the key wit the value ]. Then we can use a sub-replacement-special to have the replacement character dynamically evaluated using the provided replace dictionary.
Basically, ask vim to find the matching brace, change it, then jump back to the first brace and change it too. I would alter it slightly to this. I added a? You will also have issues if you have a right parenthesis inside a string. Which is a more complex problem to solve with regex. Sign up to join this community. The best answers are voted up and rise to the top. How to replace a pair of brackets Ask Question. Asked 11 months ago. Active 11 months ago. Viewed 2k times.
I have a file, containing some lines like this: aaa bbb ccc ddd. Yves Yves 3 3 silver badges 11 11 bronze badges. Active Oldest Votes. AndyB AndyB 3 3 bronze badges. Welcome to this site AndyB! And great answer!If your regex flavor supports lookaround, use. Here's a Python example:. If you do not want to include the brackets in the match, here's the regex:?
A positive lookahead finds a string when a certain string comes after it. A positive lookbehind finds a string when a certain string precedes it. To quote this. The parentheses capture the characters in a group.
Licensed under cc by-sa 3. Simple regex question. I have a string on the following format: this is a [sample] string with [some] special words. Tim Pietzcker 4. Can brackets be nested? If your regex flavor supports lookaround, use? LJ Germain 5. Emma 7. This code will extract the content between square brackets and parentheses?
To quote thisLook ahead positive? How does this regex work? Nezar Fadle Similar questions regex - What's the regular expression that matches a square bracket? What is the difference between square brackets and parentheses in a regex? Recent questions How to safely upgrade an Amazon EC2 instance from t1. Contact Us Licensed under cc by-sa 3.Yes, I'm using it to grok code out of sys. Thanks for sharing this beautiful post for the spring of watercolors. Assignment Editing Services.
Having trouble with your Matlab assignments? Matlab Assignment Help team got you covered. We ensure time and quality delivery at all times.
To get the best Matlab Homework Helpvisit our website now. Home About Me Contact. OK, here's the deal. As soon as the matches we desire begin to overlap, we must tweak our strategy somewhat. We can still inspect the subject for correctly-balanced groups of parentheses.
However, instead of outright matching them, we need to save them with a capturing group like so:. Exactly the same as the previous expression, except I've wrapped the bulk of it in a lookahead to avoid consuming characters, added a capturing group, and tweaked the backreference indices so they play nice with their new friend.
I'm glad you asked. The general method is quite simple: iterate through characters one at a time while simultaneously matching the next occurrences of ' ' and ' ', capturing the rest of the string in each case so as to establish positions from which to resume searching in the next iteration.
Let me break it down piece by piece: Note. Make sure ' ' follows before doing any hard work. Start of group used to iterate through the string, so the following lookaheads match repeatedly. Handle ' '. This lookahead deals with finding the next ' '. At the same time, check that there is at least another occurrence of ' '. This is a PCRE band-aid used to overcome this bug. Like the earlier ' ' match, this forces matching of a ' ' that hasn't been matched before.
The above-mentioned bug is not applicable here, so this simple expression is sufficient. Consume a single character so that the group can continue matching. It is safe to consume a character here because neither occurrence of the next ' ' or ' ' could possibly exist before the new matching point.
Match as few times as possible until a balanced group has been found. This is validated by the following check. Final validation. Match up to and including the last ' ' found.
Then match up until the position where the last ' ' was found, making sure we don't encounter another ' ' along the way which would imply an unbalanced group. Martin 10 April at Unknown 8 August at Unknown 13 February at Qasim Khan 17 August at