0

I have a string like:

hello #this# is #some text string# text text

I want change it to:

hello <sometag>this</sometag> is <sometag>some text string</sometag> text text

That is, replace the first # with an HTML tag and the second # with the closing tag, and so on. I am using Python; any body knows any regular expression or something other method?

2
  • Not sure what you want. Just the hashes removed or anything between hashes bolded? How? HTML? Commented Mar 23, 2012 at 6:55
  • edit: replace #this# inside html bold, #some text string# is inside another html bold etc Commented Mar 23, 2012 at 7:16

2 Answers 2

1

If you want to HTML bold everything separated by hashes surrounded by whitespace, you can do this:

import regex
regex = re.compile('(\s?)#(.*?)#(\s+)')
str = 'hello #this# is #some text string# text text'
str = re.sub(regex, '\\1<b>\\2</b>\\3', str)

If you want to match without space around the hashes, you change the relevant line to these:

regex = re.compile('#(.*?)#')
re.sub(regex, '<b>\\1</b>', str)

BUT this will only match paired hashes. In your example, it would result to '<b>text</b>text text<b>text</b>text#text text'

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

1 Comment

this works fine with space separated #. but how can match this #text#text text#text#text#text text
0

If you just want to remove the hashes (#) a simple replace will do the job:

 str=str.replace('#','')

1 Comment

He wants to replace the hashes with HTML tags; they weren't showing up because he didn't use code formatting.

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.