2

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!

1 Answer 1

5

I would use regular expressions to match this. Something like this could work for your expression:

String regex = "\\{.*?\\}";

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:

{tag1}/path/{tag2}/else/{tag3}

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.,

   public static void main(String[] args) {
      String test = "http://www.something.com/{tag1}/path/{tag2}/else/{tag3}.html";
      String regex = "\\{.*?\\}";

      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(test);

      while (matcher.find()) {
         System.out.println(matcher.group());
      }
   }

With an output of:

{tag1}
{tag2}
{tag3}

You can read more about regular expressions here: Oracle Regular Expressions Tutorial

and for greater detail, here: www.regular-expressions.info/tutorial

Sign up to request clarification or add additional context in comments.

9 Comments

"{[^{}]*}" might be better? (escapes were ignored)
@Kent: "{[^{}]*}" looks illegal to me. Why not test it out?
@HovercraftFullOfEels, I just omitted the escapes. well, if fit it in grep, it should be: grep -oE "\{[^{}]*\}" java needs more "\"s.
@Kent: that should work well too. I can't say though which one is "better" though.
Btw: what would be the regex to return just the insides of the {tags}, i.e. “tag1”, “tag2”, “tag3”, etc.?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.