Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am looking for a way to use Regex to replace a string like this:

The quick #[brown]brown#[clear] fox jumped over the lazy dog.

with

The quick <a style="color:#3B170B">brown<a style="color:#FFFFFF"> fox jumped over the lazy dog.

However the color code is picked out of a like as such

color_list = dict(
                 brown = "#3B170B",
                 .....
                 clear = "#FFFFFF",
                 )
share|improve this question
1  
What have you tried? –  Eric Dec 4 '12 at 15:22
    
@Eric At this point not much, I have tried using re.sub() however I cannot to my knowledge use re.group() to pull out the partiular name of the color I want. –  Blackninja543 Dec 4 '12 at 15:24
    
Do you have control over your input string? That would be a good situation in which to use string formatting if so. –  RocketDonkey Dec 4 '12 at 15:25
    
@RocketDonkey No I don't have control over the input string. –  Blackninja543 Dec 4 '12 at 15:26
1  
you miss </a> closing tag... –  Jakub M. Dec 4 '12 at 15:26

3 Answers 3

up vote 2 down vote accepted

re.sub is what you need. It takes either a replacement string or a function as its second argument. Here we provide a function because part of generating the replacement string needs is a dictionary lookup.

re.sub(r'#\[(.+?)\]', lambda m:'<a style="color:%s">' % colors[m.group(1)], s)
share|improve this answer
    
I am finding the re.sub function does not seem to work even when I implement something such as re.sub(r'#\[brown\]', colors['brown'], s) which should return the string to The quick brown #3B170Bfox#[clear] jumped over the lazy dog. –  Blackninja543 Dec 4 '12 at 16:11
    
I renamed color_list to colors. Are you forgetting to update that name? –  Steven Rumbalski Dec 4 '12 at 17:02
    
No in fact I used a completely different name in my code and was thrown an error to fix it, after the fix there was no change in the string. –  Blackninja543 Dec 4 '12 at 17:17
    
@Blackninja543: Are you assigning the result of re.sub back to your string? –  Steven Rumbalski Dec 4 '12 at 17:53
    
No.... just fixed it thanks. –  Blackninja543 Dec 4 '12 at 18:20

Just one line of fine python should help:

reduce(lambda txt, i:txt.replace('#[%s]'%i[0],'<a style="color=%s;">'%i[1]),colors.items(),txt)
share|improve this answer
1  
Looks like Haskell-derived perfectly unreadable one-liner : ) . Although I like the idea –  Jakub M. Dec 4 '12 at 15:29

A crude pseudo-python solution would look like this:

for key, value in color_list.items()
  key_matcher = dict_key_to_re_pattern( key )
  formatted_value = '<a style...{0}...>'.format( value )
  re.sub( key_matcher, formatted_value, your_input_string )


def dict_key_to_re_pattern( key ):
   return r'#[{0}]'.format( key )
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.