1

I'm trying to get the numbers/stings out of a string that looks like this

"[123][456][abc]"

Also I don't want to include "[" or "]" and I want to keep the values separate.

3
  • [(([\d]|[a-z])+)] But in includes "]" and I'm not sure how to specify which group Commented Mar 3, 2011 at 23:15
  • the language you are using would be a lot of help Commented Mar 3, 2011 at 23:23
  • What do you means by "includes"? The only thing included is what's within the capturing parens. Also,what you wrote isn't valid -- those outer brackets need to be escaped. As for "which group" ... these patterns will match any group; pulling out specific groups depends upon your programming language. Commented Mar 3, 2011 at 23:25

3 Answers 3

1

Try this on for size.

/\[(\d+|[a-zA-Z]+)\]/

Edit:

If you can support lookahead and lookbehind

/(?<=\[)(\d+|[a-zA-Z]+)(?=\])/

Another edit:

try this in javascript

var text = "[12][34][56][bxe]";
var array = text.match(/(\d+|[a-zA-Z]+)/g);
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, that works but how would i get the second and third group?
@Pardoner Capture groups can be assigned to either scalar variables or to an array, or can be looped over. Simply take the ones you want.
@Kyle: g stands for global, not greedy. It causes the regex to be applied repeatedly until no more matches are found. What your code does is replace each chunk of digits or letters with itself resulting in the same string you started with (not an array).
@Alan global, correct. This code is meant to illustrate what the regular expression will capture. String.match does not accept a function as a second argument so I used String.replace. This will return an array of the desired matches. var text = "[12][34][56][bxe]"; var array = text.match(/(\d+|[a-zA-Z]+)/g);
1

This would be a lot easier if we knew the language. For example, in Javascript you can do:

"[123][456][abc]".split(/[\[\]]/);

and similarly in Python:

>>> import re
>>> re.split(r'[\[\]]', "[123][456][abc]")
['', '123', '', '456', '', 'abc', '']

I'm sure there are ways to do this in other languages, too.

1 Comment

You mean you're trying to do it with regex and without split, right? Because I did use regex.
1

See http://www.regular-expressions.info/javascript.html, particularly the "How to Use The JavaScript RegExp Object" section:

If you want to retrieve the part of the string that was matched, call the exec() function of the RegExp object that you created, e.g.: mymatch = myregexp.exec("subject"). This function returns an array. The zeroth item in the array will hold the text that was matched by the regular expression. The following items contain the text matched by the capturing parentheses in the regexp, if any. mymatch.length indicates the length of the match[] array, which is one more than the number of capturing groups in your regular expression. mymatch.index indicates the character position in the subject string at which the regular expression matched. mymatch.input keeps a copy of the subject string.

That explains how to access individual parenthesized groups. You can use that in conjunction with a pattern like /\[(\w+)\]/g

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.