2011-04-20 12:29:01 +00:00
<?xml version="1.0" encoding="utf-8"?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" lang = "fr" xml:lang = "fr" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" / >
< meta name = "keywords" content = "regexp, regular expression" >
2011-04-20 13:56:52 +00:00
< link rel = "shortcut icon" type = "image/x-icon" href = "/Scratch/img/favicon.ico" / >
< link rel = "stylesheet" type = "text/css" href = "/Scratch/assets/css/main.css" / >
2012-04-02 21:43:39 +00:00
< link rel = "stylesheet" type = "text/css" href = "/Scratch/css/solarized.css" / >
2011-04-20 13:56:52 +00:00
< link rel = "stylesheet" type = "text/css" href = "/Scratch/css/idc.css" / >
2012-05-02 15:43:56 +00:00
< link href = 'http://fonts.googleapis.com/css?family=Inconsolata' rel = 'stylesheet' type = 'text/css' >
2011-04-20 12:29:01 +00:00
< link rel = "alternate" type = "application/rss+xml" title = "RSS" href = "http://feeds.feedburner.com/yannespositocomen" / >
2011-04-20 13:56:52 +00:00
< link rel = "alternate" lang = "fr" xml:lang = "fr" title = "Tout sauf quelquechose en expression régulière." type = "text/html" hreflang = "fr" href = "/Scratch/fr/blog/2010-02-16-All-but-something-regexp--2-/" / >
< link rel = "alternate" lang = "en" xml:lang = "en" title = "Pragmatic Regular Expression Exclude (2)" type = "text/html" hreflang = "en" href = "/Scratch/en/blog/2010-02-16-All-but-something-regexp--2-/" / >
< script type = "text/javascript" src = "/Scratch/js/jquery-1.3.1.min.js" > < / script >
< script type = "text/javascript" src = "/Scratch/js/jquery.cookie.js" > < / script >
< script type = "text/javascript" src = "/Scratch/js/index.js" > < / script >
2012-05-02 15:43:56 +00:00
< script type = "text/javascript" src = "/Scratch/js/highlight/highlight.pack.js" > < / script >
< script type = "text/javascript" src = "/Scratch/js/article.js" > < / script >
2011-04-20 12:29:01 +00:00
<!-- [if lt IE 9]>
< script src = "http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js" > < / script >
<![endif]-->
< title > Pragmatic Regular Expression Exclude (2)< / title >
< / head >
2011-10-18 22:30:00 +00:00
< body lang = "en" class = "article" >
2011-04-20 12:29:01 +00:00
< script type = "text/javascript" > / / < ! [ C D A T A [
2011-04-20 13:56:52 +00:00
document.write('< div id = "blackpage" > < img src = "/Scratch/img/loading.gif" alt = "loading..." / > < / div > ');
2011-04-20 12:29:01 +00:00
// ]]>
< / script >
< div id = "content" >
< div id = "choix" >
< div class = "return" > < a href = "#entete" > ↓ Menu ↓ < / a > < / div >
< div id = "choixlang" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/fr/blog/2010-02-16-All-but-something-regexp--2-/" onclick = "setLanguage('fr')" > en Français< / a >
2011-04-20 12:29:01 +00:00
< / div >
2011-09-28 16:05:55 +00:00
< div class = "flush" > < / div >
2011-04-20 12:29:01 +00:00
< / div >
< div id = "titre" >
< h1 >
Pragmatic Regular Expression Exclude (2)
< / h1 >
< / div >
< div class = "flush" > < / div >
< div class = "flush" > < / div >
< div id = "afterheader" >
< div class = "corps" >
2012-05-03 09:21:34 +00:00
< p > In my < a href = "previouspost" > previous post< / a > I had given some trick to match all except something. On the same idea, the trick to match the smallest possible string. Say you want to match the string between ‘ a’ and ‘ b’ , for example, you want to match:< / p >
2011-04-20 12:29:01 +00:00
< pre class = "twilight" >
a.....< span class = "Constant" > < strong > a......b< / strong > < / span > ..b..a....< span class = "Constant" > < strong > a....b< / strong > < / span > ...
< / pre >
< p > Here are two common errors and a solution:< / p >
< pre class = "twilight" >
/a.*b/
< span class = "Constant" > < strong > a.....a......b..b..a....a....b< / strong > < / span > ...
< / pre >
< p > The first error is to use the < em > evil< / em > < code > .*< / code > . Because you will match from the first to the last.< / p >
< pre class = "twilight" >
/a.*?b/
< span class = "Constant" > < strong > a.....a......b< / strong > < / span > ..b..< span class = "Constant" > < strong > a....a....b< / strong > < / span > ...
< / pre >
< p > The next natural way, is to change the < em > greediness< / em > . But it is not enough as you will match from the first < code > a< / code > to the first < code > b< / code > .
2012-05-03 09:21:34 +00:00
Then a simple constatation is that our matching string shouldn’ t contain any < code > a< / code > nor < code > b< / code > . Which lead to the last elegant solution.< / p >
2011-04-20 12:29:01 +00:00
< pre class = "twilight" >
/a[^ab]*b/
a.....< span class = "Constant" > < strong > a......b< / strong > < / span > ..b..a....< span class = "Constant" > < strong > a....b< / strong > < / span > ...
< / pre >
< p > Until now, that was, easy.
Now, just pass at the case you need to match not between < code > a< / code > and < code > b< / code > , but between strings.
For example:< / p >
2012-05-02 15:43:56 +00:00
< div >
< pre > < code class = "perl" > < li> ...< li>
< / code > < / pre >
< / div >
2011-04-20 12:29:01 +00:00
< p > This is a bit difficult. You need to match < / p >
2012-05-02 15:43:56 +00:00
< div >
< pre > < code class = "perl" > < li> [anything not containing < li> ]< /li>
< / code > < / pre >
< / div >
2011-04-20 12:29:01 +00:00
< p > The first method would be to use the same reasoning as in my < a href = "previouspost" > previous post< / a > . Here is a first try:< / p >
2012-05-02 15:43:56 +00:00
< div >
< pre > < code class = "perl" > < li> ([^< ]|< [^l]|< l[^i]|< li[^> ])*< /li>
< / code > < / pre >
< / div >
2011-04-20 12:29:01 +00:00
< p > But what about the following string: < / p >
2012-05-02 15:43:56 +00:00
< div >
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
< pre > < code class = "perl" > < li> ...< li< /li>
< / code > < / pre >
< / div >
2012-05-03 09:21:34 +00:00
< p > That string should not match. This is why if we really want to match it correctly< sup > < a href = "#note1" > † < / a > < / sup > we need to add:< / p >
2012-05-02 15:43:56 +00:00
< div >
< pre > < code class = "perl" > < li> ([^< ]|< [^l]|< l[^i]|< li[^> ])*(|< |< l|< li)< /li>
< / code > < / pre >
< / div >
2011-04-20 12:29:01 +00:00
< p > Yes a bit complicated. But what if the string I wanted to match was even longer?< / p >
< p > Here is the algorithm way to handle this easily. You reduce the problem to the first one letter matching:< / p >
2012-05-02 15:43:56 +00:00
< div >
< pre > < code class = "perl" > # transform a simple randomly choosen character
# to an unique ID
# (you should verify the identifier is REALLY unique)
# beware the unique ID must not contain the
# choosen character
s/X/_was_x_/g
s/Y/_was_y_/g
# transform the long string in this simple character
s/< li> /X/g
s/< \/li> /Y/g
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
# use the first method
s/X([^X]*)Y//g
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
# retransform choosen letter by string
s/X/< li> /g
s/Y/< \/li> /g
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
# retransform the choosen character back
s/_was_x_/X/g
s/_was_y_/Y/g
< / code > < / pre >
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
< / div >
2011-04-20 12:29:01 +00:00
< p > And it works in only 9 lines for any beginning and ending string. This solution should look less < em > I AM THE GREAT REGEXP M45T3R, URAN00B< / em > , but is more convenient in my humble opinion. Further more, using this last solution prove you master regexp, because you know it is difficult to manage such problems with only a regexp.< / p >
< hr / >
2012-05-03 09:21:34 +00:00
< p > < small > < a name = "note1" > < sup > † < / sup > < / a > I know I used an HTML syntax example, but in my real life usage, I needed to match between < code > en:< / code > and < code > ::< / code > . And sometimes the string could finish with < code > e::< / code > .< / small > < / p >
2011-04-20 12:29:01 +00:00
< / div >
2012-04-10 13:56:34 +00:00
< div id = "social" >
< div class = "left" > < a href = "https://twitter.com/share" class = "twitter-share-button" data-via = "yogsototh" > Tweet< / a >
< script > ! function ( d , s , id ) { var js , fjs = d . getElementsByTagName ( s ) [ 0 ] ; if ( ! d . getElementById ( id ) ) { js = d . createElement ( s ) ; js . id = id ; js . src = "//platform.twitter.com/widgets.js" ; fjs . parentNode . insertBefore ( js , fjs ) ; } } ( document , "script" , "twitter-wjs" ) ; < / script >
< / div >
< div class = "left" > < div class = "g-plusone" data-size = "medium" data-annotation = "inline" data-width = "106" > < / div >
< script type = "text/javascript" >
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
< / script >
< / div >
< div class = "flush" > < / div >
< / div >
2011-04-20 12:29:01 +00:00
< div id = "choixrss" >
< a id = "rss" href = "http://feeds.feedburner.com/yannespositocomen" >
Subscribe
< / a >
< / div >
< script type = "text/javascript" >
$(document).ready(function(){
$('#comment').hide();
$('#clickcomment').click(showComments);
});
function showComments() {
$('#comment').show();
$('#clickcomment').fadeOut();
}
2012-04-10 13:56:34 +00:00
document.write('< div id = "clickcomment" > Comments & Share< / div > ');
2011-04-20 12:29:01 +00:00
< / script >
< div class = "flush" > < / div >
2012-04-10 13:56:34 +00:00
2011-04-20 12:29:01 +00:00
< div class = "corps" id = "comment" >
< h2 class = "first" > comments< / h2 >
< noscript >
You must enable javascript to comment.
< / noscript >
< script type = "text/javascript" >
var idcomments_acct = 'a307f0044511ff1b5cfca573fc0a52e7';
2011-04-20 13:56:52 +00:00
var idcomments_post_id = '/Scratch/en/blog/2010-02-16-All-but-something-regexp--2-/';
var idcomments_post_url = 'http://yannesposito.com/Scratch/en/blog/2010-02-16-All-but-something-regexp--2-/';
2011-04-20 12:29:01 +00:00
< / script >
< span id = "IDCommentsPostTitle" style = "display:none" > < / span >
2011-04-20 13:56:52 +00:00
< script type = 'text/javascript' src = '/Scratch/js/genericCommentWrapperV2.js' > < / script >
2011-04-20 12:29:01 +00:00
< / div >
< div id = "entete" class = "corps_spaced" >
< div id = "liens" >
2011-04-20 13:56:52 +00:00
< ul > < li > < a href = "/Scratch/en/" > Home< / a > < / li >
< li > < a href = "/Scratch/en/blog/" > Blog< / a > < / li >
< li > < a href = "/Scratch/en/softwares/" > Softwares< / a > < / li >
< li > < a href = "/Scratch/en/about/" > About< / a > < / li > < / ul >
2011-04-20 12:29:01 +00:00
< / div >
< div class = "flush" > < / div >
< hr / >
< div id = "next_before_articles" >
< div id = "previous_articles" >
previous entries
< div class = "previous_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/en/blog/2010-02-15-All-but-something-regexp/" > < span class = "nicer" > «< / span > Pragmatic Regular Expression Exclude< / a >
2011-04-20 12:29:01 +00:00
< / div >
< div class = "previous_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/en/blog/2010-01-12-antialias-font-in-Firefox-under-Ubuntu/" > < span class = "nicer" > «< / span > antialias font in Firefox under Ubuntu< / a >
2011-04-20 12:29:01 +00:00
< / div >
< div class = "previous_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/en/blog/2010-01-04-Change-default-shell-on-Mac-OS-X/" > < span class = "nicer" > «< / span > Change default shell on Mac OS X< / a >
2011-04-20 12:29:01 +00:00
< / div >
< / div >
< div id = "next_articles" >
next entries
< div class = "next_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/en/blog/2010-02-18-split-a-file-by-keyword/" > split a file by keyword < span class = "nicer" > »< / span > < / a >
2011-04-20 12:29:01 +00:00
< / div >
< div class = "next_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/en/blog/2010-02-23-When-regexp-is-not-the-best-solution/" > When regexp is not the best solution < span class = "nicer" > »< / span > < / a >
2011-04-20 12:29:01 +00:00
< / div >
< div class = "next_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/en/blog/2010-03-22-Git-Tips/" > Git Tips < span class = "nicer" > »< / span > < / a >
2011-04-20 12:29:01 +00:00
< / div >
< / div >
< div class = "flush" > < / div >
< / div >
< / div >
< div id = "bottom" >
2012-04-02 21:43:39 +00:00
< div >
2012-04-10 13:56:34 +00:00
< a href = "https://twitter.com/yogsototh" > Follow @yogsototh< / a >
2012-04-02 21:43:39 +00:00
< / div >
2011-04-20 12:29:01 +00:00
< div >
< a rel = "license" href = "http://creativecommons.org/licenses/by-sa/3.0/" > Copyright ©, Yann Esposito< / a >
< / div >
< div id = "lastmod" >
Created: 02/16/2010
2011-04-20 13:09:17 +00:00
Modified: 04/20/2011
2011-04-20 12:29:01 +00:00
< / div >
< div >
Entirely done with
< a href = "http://www.vim.org" > Vim< / a >
and
< a href = "http://nanoc.stoneship.org" > nanoc< / a >
< / div >
< / div >
< div class = "clear" > < / div >
< / div >
< / body >
< / html >