Does anyone have a good regex to do this? For example:
This is *an* example
should become
This is <b>an</b> example
I need to run this in Objective C, but I can probably work that bit out on my own. It’s the regex that’s giving me trouble (so rusty…). Here’s what I have so far:
s/\*([0-9a-zA-Z ])\*/<b>$1<\/b>/g
But it doesn’t seem to be working. Any ideas? Thanks 🙂
EDIT: Thanks for the answer 🙂 If anyone is wondering what this looks like in Objective-C, using RegexKitLite:
NSString *textWithBoldTags = [inputText stringByReplacingOccurrencesOfRegex:@"\\*([0-9a-zA-Z ]+?)\\*" withString:@"<b>$1<\\/b>"];
EDIT AGAIN: Actually, to encompass more characters for bolding I changed it to this:
NSString *textWithBoldTags = [inputText stringByReplacingOccurrencesOfRegex:@"\\*([^\\*]+?)\\*" withString:@"<b>$1<\\/b>"];
You’re only matching one character between the
*s. Try this:or to ensure there’s at least one character between the
*s: