1

I have following text:

my-widget{ color: #{mycolors.getColors(1)} } ... my-tridget{ color: #{mycolors.getColors(2)} ... } ... 

I want to split the text in pairs, where the delimiter is #{mycolors.getColors()} and the text between previous delimeter and current delimiter will be saved. E.g. for such pairs:

Pair 1: text: my-widget{ color: number: 1

Pair 2: text: } ... my-tridget{ color: number: 2

What I am used so far,

 Pattern p = Pattern.compile("(.*)#\\{mycolors.getColor\\(([0-9])\\)\\}", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE); Matcher m = p.matcher(data); while (m.find){ String number = m.group(2).toLowerCase().trim(); String text = m.group(1); } 

But number and text will be to:

text: color: number: 1

text: color: number: 2

So the text doesn't go over several lines. How can I achieve this ? (The Pattern.DOTALL in addtion to Pattern.MULTILINE didn't help me)

2
  • stackoverflow.com/questions/3651725/… Commented Sep 16, 2013 at 12:21
  • @Simon Sorry but I don't know how I can use your suggestion. Commented Sep 16, 2013 at 12:31

1 Answer 1

1

Some mistakes you're making:

  1. To match text across multiple lines you need to use Pattern.DOTALL instead of Pattern.MULTILINE
  2. Instead of .* make it non greedy .*?
  3. Your text has string getColors but you have getColor in your regex

Following regex pattern should work out for you:

Pattern p = Pattern.compile("(.*?)#\\{mycolors.getColors\\((\\d+)\\)\\}", Pattern.CASE_INSENSITIVE|Pattern.DOTALL); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer! I've tried to change the regex and it seems to do what I want to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.