So come up with the regex pattern for this. Instead, instruct the regex to match (and capture) all pattern instances in the string, what can be done in any regex implementation (language). The problem with the attempted code, as discussed, is that there is one capture group matching repeatedly so in the end only the last match can be kept. For instance in ruby I'd use something like str.split(",") or str.scan(/+/) IMHO, a regular expression is not one of them. That being said, I'd suggest not using regular expression in that case, there are many other great tools from a simple split to some tokenization patterns depending on your needs. Since I'm not much of a swift guy, here's a ruby example: def make_regexp(group_regexp, count: 3, delimiter: ",") You can see a fully detailed explanation about this regular expression on Regex101.Īs I said, it is pretty easy to generate this regexp for any groups you want using your favorite language. Will match the next sentences, with one, two or three capturing groups. For instance, if I want to match between 1 and 3 words, the regexp: ^(+)(?:,(+))?(?:,(+))?$ You can generate a regexp that will match either n words, as long as your n is predetermined. After reading Byte Commander's answer, I want to introduce a tiny possible improvement:
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |