Saturday, 17 February 2018

sublimetext2 - Regex for varying tags?



Not sure I worded the question correctly as I am new to Regex. I have a regex for a syntax I am working on for BBCode in sublime. This regex matches fine when used in this manner:



(\[color=red?(.*?)\])(.*?)(\[/color\])



[color=red]Red Text[/color]




That works great for matching/grouping the code. But how would I go about capturing varying and extra tags in the match? For example if the bbcode would read:




[center][size=6][color=red][b]Red Text[/b][/color][/size][/center]
or



[center][size=6][color=red][i]Red Text[/i][/color][/size][/center]




That regex would capture the [b] and [i] tags within the 3rd group. How would I split the [b]/[i] tags to another group and include a 'varying' capture of [u][b][i] if it was used? The order wouldn't be consistent either. Not sure how this would be done.


Answer



This language is not regular, i.e. it can not be expressed using regular expressions.
You need a grammar and a parser to capture this language. See more here.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...