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.
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.
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);
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).var text = "[12][34][56][bxe]"; var array = text.match(/(\d+|[a-zA-Z]+)/g);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.
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