Saturday, February 17, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...