简体   繁体   中英

regex for nested values

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);

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, eg: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM