/    Sign up×
Community /Pin to ProfileBookmark

regular expression: efficiency

Hi there,

I haven’t much experience with regular expressions.
I wanted to re-format strings, for example in replacing “_underlined text_” with “<u>underlined text</u>”

I draw up the following rules:

[QUOTE]

1) the “_“-character (open tag) must be after a whitespace or a newline
and before an alphanumeric-character
2) this can be followed by any characters (exept line-breaks)
3) the “_
“-character (close tag) must be after an alphanumeric character
and before a whitespace or an endline

[/QUOTE]

and I got the following regexp:

[CODE]#( |^)_([a-z0-9]+)([^<>n]*?)([a-z0-9]+)_( |$)#im[/CODE]

But I think there are more efficient and nicer ways (perhaps there are simpler ways? (what means that they are faster))

thanks ?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 30.2011 — [code=php]
$text = preg_replace('#b_([^<>]*?)_b#s', '<u>$1</u>', $text);
[/code]
Copy linkTweet thisAlerts:
@tha_oneauthorJul 01.2011 — nice, thank you

I modified it, because your code did also match stand-alone '_' and it matched line-breaks:

[CODE]#b_B([^<>n]*?)B_b#[/CODE]
×

Success!

Help @tha_one spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...