Search
  • Derek Carlson

Regular Expressions

I spent 36 years programming before I stopped and read the seminal book “Mastering Regular Expressions” by Friedl. Prior to that I always took a deep breath and prepared for battle when I had to solve a programming problem using regular expressions.


Today's challenge: How do you change stuff like this -

<p class="headc" id="book1_III"><b>III. S<small>ÜHNE</small> und Wunder</b></p>

to this -

<ihead> <inr>III.</inr> <ih>S<small>ÜHNE</small> und Wunder</ih> </ihead>

Answer:


s/<p class="headc"\s+id="book[^"]+"><b>([^.]+?)\.\s*([^<]+)(<small>[^<]+<\/small>)*([^<]*)<\/b><\/p>/<ihead>\n <inr>$1.<\/inr> <ih>$2$3$4<\/ih>\n<\/ihead>/g


And if any of those characters is wrong or misplaced in that spaghetti mess above, it won't work.


Hence:


Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -Jamie Zawinski





8 views0 comments

Recent Posts

See All