top of page
Search

Regular Expressions

Writer's picture: Derek CarlsonDerek Carlson

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





11 views0 comments

Recent Posts

See All

"Weeds"?

Comments


Thank you so much!  I'll be in touch soon to personally say hi.

From time to time I email out something I learned that might be helpful to some of you out there.

If you want to stay in the loop in this way, just provide your email address below.

© 2016-2024 Conscious Computer Consulting

bottom of page