What would be a convenient and reliable way to extract all the “{…}” tags from a given string? (Using Java).
So, to give an example:
Say I have: http://www.something.com/{tag1}/path/{tag2}/else/{tag3}.html
I want to get all the “{}” tags; I was thinking about using the Java .split() functions, but not sure what the correct regex would be for this.
Note also: tags can be called anything, not just tagX!
I would use regular expressions to match this. Something like this could work for your expression:
As this will “reluctantly” match any sub string that has { and } surrounding it. The
.*?makes it find any character between the { and }, but reluctantly, so it doesn’t match the bigger String:which would be a “greedy” match. Note that the curly braces in the regex need to be escaped with double backslashes since curly braces have a separate meaning inside a regular expression, and if you want to indicate the curly brace String, you need to escape it.
e.g.,
With an output of:
You can read more about regular expressions here:
Oracle Regular Expressions Tutorial
and for greater detail, here:
http://www.regular-expressions.info/tutorial