Please navigate to the bottom of the page for Table of Contents

Friday, July 27, 2012

Regular Expressions interview questions–part 2

In our previous post, we talked about regular expression character sets, groups, quantifiers, shorthand and gives a full solution to matching a a 10-digit, US phone number, with or without parentheses, hyphens, or dots and optional 3 digit area code. In this post, we well dive deeper into some examples of pattern matching.

Regular expressions at their core are all about pattern finding and matching – strings, digits, letters or characters.

Question: Show different ways of matching digits and non-digits using regular expressions.

There are many ways to match a single digit

  • \d
  • [0-9]
  • [0123456789]
  • [012] – matches to either 0, 1 or 2

Similarly, there are many ways to match to a non-digit.

  • \D
  • [^0-9]
  • [^\d]

A couple of notes:

  1. ^ signals negation of the expression to the processor.
  2. \D matches whitespace, punctuation, quotation marks, hyphens, forward slashes, square brackets, and other similar characters.

Question: Explain the different ways to match words and non-words.

  •  \w is the main character shorthand for matching letters and numbers. Consider it to match alpha numeric characters.
  • [a-zA-Z0-9] is the same as \w.
  • Note that \D matches whitespace also; \w does not.
  • \W (capital W) matches non-word matches - whitespace, punctuation, and other kinds of characters that
    aren’t used in words.
  • [^a-zA-Z0-9] is the same as \W (capital).

Question: Write a regular expression to match anything that is a non-space.

  • Space (blank) can be matched by \s. So to match non-space character, we can use \S.
  • Another equivalent way to match non-space would be [^\s]
  • Yet another way would be [^ \t\n\r]

Question: Write a regular expression to match words that are exactly 7 characters in length and start with P and end with D.

In this question, the interviewer is testing your ability to understand word boundaries and repeat notations. The following expression solves this question: \bP.{5}D\b.

To dissect the expression:

  • The shorthand \b matches a word boundary, without consuming any characters.
  • The characters P and D also bound the sequence of characters.
  • .{5} matches any five characters.
  • Match another word boundary with \b.

92 comments:

  1. The above solution does not work if there are multiple 7 character words starting with P and ending in D. It only returns the first such word

    ReplyDelete
  2. use g identifier as in m/$pattern/g;

    ReplyDelete
  3. this will work.[P]\B.{5}[D]\b

    ReplyDelete
  4. It’s going to be finish of mine day, however before finish I am reading this fantastic post to increase my knowledge.

    I love this site – its so usefull and helpfull Pakar Seo this website is extremely helpful Pakar Seo its so usefull and helpfull Pakar Seo I just want to say I’m newbie to blogging and site-building and actually liked this blog site. Pakar Seo Very likely I’m planning to bookmark your blog post . You amazingly have awesome article content. Regards for revealing your blog site. Pakar Seo ....

    ReplyDelete
  5. Ni Hau,


    Allow me to show my gratitude bloggers. You guys are like unicorns. Never seen but always spreading magic. Your content is yummy. So satisfied.



    I'd like to post some questions
    concerning the next listed code.
    The code is from the Core Java book (the second volume).
    There are two classes: TreeSetTest and class Item which implements Comparable.
    The first class, which contains the main method is straight foreward and does not
    cause any problems for me.




    I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.


    Best Regards,
    Ajeeth

    ReplyDelete
  6. Hi,

    I want query for one question i.e if
    input:firstname.lastname@gmail.com
    output:lastname gmail.com
    please reply asap.

    ReplyDelete
  7. Main dan dapatkan keuntungan uang asli dengan bermain poker online, daftarkan sekrang juga dengan cari disini , what are you waiting for ?

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. DewiDomino merupakan Agen Domino QQ Online Resmi Terpercaya Indonesia dan Situs Judi Poker V 88 Uang Asli Deposit Via Pulsa,Bandar Capsa Susun,Bandarq Sakong Adu Q Domino99 dan Ceme Terbaik hanya di http://w88messi.biz

    Agen Poker Resmi
    BandarQ Indonesia
    Agen Poker Online
    Poker V
    Seo Online

    ReplyDelete
  10. Amazing and lovely, I like it and i will share it to others. Join us at http://bolanet88.biz

    Agen Bola Indonesia
    Casino Online
    Aplikasi Judi Bola Online
    Situs Bola

    ReplyDelete
  11. great post you have guys and i really like it.. please visit us at http://jampoker.co

    Agen Idn Poker
    Poker Online Terpercaya
    Situs Poker Indonesia
    Seo Online

    ReplyDelete
  12. Yukk Gabung Bersama MileniaPoker - Poker Deposit Pulsa Dengan fitur - fitur lengkap serta Customer Service yang 24 jam. Poker Online

    ReplyDelete
  13. Mari Gabung sekarang juga di MileniaPoker - Situs Poker terbaik di Indonesia.

    Game Poker Online
    Poker Online CC
    Situs Poker Online Terpopuler
    MileniaPoker

    ReplyDelete
  14. yuk daftar sekarang juga di INDOBET dimana Indo36 terpilih sebagai Bandar Judi Slot Online terbagus dan terbaik yang ada di Indonesia.

    Segera gabung bersama kami dan dapatkan kesempatan memenangkan hadiah jutaan rupiah. Indo36 sebagai Situs Judi Slot Online terbaik juga menyediakan layanan Judi Deposit Pulsa XL,Axis dan Telkomsel yang dapat digunakan sebagai alternatif transaksi

    ReplyDelete
  15. Whilst looking for a charging spot for my electric car it came to me. When it comes to buying seafood, it just simply isn’t worth trying to save a few pounds choosing dredged over hand-caught scallops. Add that to the list of promises Obama has broken.

    sbobet mobile
    situs poker online
    poker online
    Adu Ayam
    bonus deposit poker
    game online domino

    ReplyDelete
  16. Merupakan Situs Judi Sbobetuk Online Terbaik dan Terpercaya di Indonesia. Sebagai Bandar terekomendasi untuk para bettor di Indonesia, di sini anda akan mendapatkan segala kemudahan untuk bertaruh.

    ReplyDelete
  17. http://BandarXL.net merupakan Agen Slot Online Resmi Indonesia Deposit Pakai Pulsa Via OVO, Telkomsel, XL, dan GOPAY di Asia yang menyediakan permainan Situs Slot, Casino Online, Bandar Taruhan Bola Sbobet Terpercaya, Poker Online, Togel, Keno, dan Lotere.

    Situs Slot
    Deposit Pakai Pulsa
    Agen Slot Online
    Judi Casino Indonesia
    Jasa Seo Online

    ReplyDelete
  18. Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know

    Agen Poker IDN

    ReplyDelete
  19. I’m honored to obtain a call coming from a friend as he discovered the important recommendations shared on your own site. Browsing your blog posting is a real wonderful experience.
    Thanks again for thinking of readers just like
    me, and I want for you the best of achievements like a professional in this discipline.

    Game Domino Online

    domino player
    master domino
    rocang bonuses
    card gaple
    domino agents

    ReplyDelete
  20. Merupakan Situs Judi Agen Sbobet Online Terbaik dan Terpercaya di Indonesia. Sebagai Bandar terekomendasi untuk para bettor di Indonesia, di sini anda akan mendapatkan segala kemudahan untuk bertaruh.

    ReplyDelete
  21. Segera daftarkan diri anda sekarang juga di QQ1221 - Bandar Judi Online Terbesar dan Terpercaya di Indonesia.

    Link Alternatif QQ1221

    ReplyDelete
  22. Situs Pasang Permainan IDN Poker Online Terpercaya di Indonesia, dengan minimal deposit dan withdraw cukup 10 ribu saja loh... segera bergabung ya dan menangkan jackpot ratusan juta...

    Situs Poker Online
    Poker Online
    s128
    Baccarat Online

    ReplyDelete
  23. INDIPOKER merupakan Situs layanan Judi Online dan Agen IDN Poker 88 Play Terpercaya Dengan Permainan Ceme Keliling,Domino Qiu Qiu,Bandar QQ dan Capsa Susun

    Agen Idn Poker
    Poker Online Terpercaya
    Bandar Ceme
    Ceme Keliling
    Layanan Judi Online

    ReplyDelete
  24. W88XL merupakan Agen Slot Online Resmi Indonesia Deposit Pakai Pulsa Via OVO, Telkomsel, XL, dan GOPAY di Asia yang menyediakan permainan Situs Slot, Casino Online, Bandar Taruhan Bola Sbobet Terpercaya, Poker Online, Togel, Keno, dan Lotere.

    Casino Online
    Slot Online
    Situs Judi
    Agen Bola Resmi Indonesia
    Seo Online

    ReplyDelete
  25. Mainkan Permainan Togel Online Terbaik dan Terlengkap di Indonesia

    Bandar Judi Togel Online
    Togel Deposit Pulsa

    ReplyDelete
  26. DewiDomino merupakan Agen Domino QQ Online Resmi Terpercaya Indonesia dan Situs Judi Poker V 88 Uang Asli Deposit Pakai Pulsa,Bandar Capsa Susun,Bandarq Sakong Adu Q Domino99 dan Ceme Terbaik

    Situs Judi Poker
    Deposit Pakai Pulsa
    Poker Online Terpercaya
    Agen Poker Indonesia
    Seo Online

    ReplyDelete
  27. BandarXL merupakan Agen Slot Online Resmi Indonesia Deposit Pakai Pulsa Via OVO, Telkomsel, XL, dan GOPAY di Asia yang menyediakan permainan Situs Slot, Casino Online, Bandar Taruhan Bola Sbobet Terpercaya, Poker Online, Togel, Keno, dan Lotere.

    Agen Slot Pulsa
    Slot Pakai Pulsa
    Situs Slot Online Pulsa
    Judi Casino Via Pulsa
    Casino Online
    Jasa Seo

    ReplyDelete
  28. situs judi online sangatlah penting sekarang ini bagi kita dalam menentukan situs mana yang akan kita percayai untuk bermain judi online . Situs judi online ini juga dikenal oleh para pemain judi sebagai bandar togel terpercaya adalah salah satu bandar terbaik pilihan utama pecinta judi online. Dengan 1ID akuAn yang kita daftarkan kita sudah dapat bermain semua permainan judi online di situs ini. Berbagai permainan judi online seperti: slot online , poker online , casino online, judi sportbook seperti judi bola, baskel, badminton, dll dan juga tidak ketinggalan judi togel online yang digemari pemain judi di Indonesia. Pasaran togel resmi yang tersedia seperti togel hongkong pools , togel singapore, togel sydney, togel pcso, togel bullseye and tidak lupa juga judi IDN poker

    ReplyDelete
  29. This comment has been removed by the author.

    ReplyDelete
  30. MPO007 sebagai situs judi online resmi dan terpercaya di Indonesia, sejak berdiri telah memberikan perlayanan terbaik dalam dunia judi online. Berbagai permainan terbaik bisa dimainkan dengan menggunakan 1 ID. Seperti slot online , poker online, judi bola, casino online, togel online dan tidak lupa juga bonus terbaik buat setiap pemain judi online yang bermain bersama MPO007

    ReplyDelete
  31. Bermain game judi poker qq online yang terpercaya di https://regenbox.org/forums/users/badutqq/ akan bisa banyak memberikan kemenangan dan keuntungan yang besar.

    ReplyDelete
  32. Bergabung bermain pada situs judi poker qq online terpercaya yang ada di http://e-journal.unipma.ac.id/index.php/JPAUD/user/viewPublicProfile/18569 akan bisa memberikan banyak keuntungan yang besar serta kemenangan yang besar.

    ReplyDelete
  33. Total4D adalah bandar judi dan Agen Togel Online terbesar dan terpercaya mulai dari angka jitu Togel Singapore,hongkong,sidney dan data info keluaran togel

    Agen Togel Online
    Togel Online
    Togel Singapore
    Bandar Togel Terpercaya

    ReplyDelete
  34. pokerqq adalah permainan poker, dominoqq, bandarq online terbaik dengan deposit 15ribu.
    https://isitup.org/94.46.58.31

    ReplyDelete
  35. Situs Poker DuetQQ dengan aneka game yang sangat menarik. Diantaranya DominoQQ sangat populer untuk dimainkan.

    ReplyDelete
  36. qq online memiliki beranekaragam permainan seperti dominoqq, bandarq, aduq hingga bandar66.
    https://isitup.org/situs8poker.com

    ReplyDelete
  37. situs poker terpercaya paling murah dan menguntungkan karena bisa menang jutaan rupiah.
    https://isitup.org/94.46.60.6

    ReplyDelete
  38. ListbandarQiu adalah website dengan kumpulan informasi Situs Judi Online yang sudah banyak dipercayai oleh semua pemain yang ada.
    Web : https://isitup.org/94.46.120.102

    ReplyDelete
  39. I really enjoyed what you had to say. Keep going because you definitely bring a new voice to this subject. Not many people would say what youve said and still make it interesting. Well, at least Im interested. Cant wait to see more of this from you.
    daftar game terbaik

    ReplyDelete
  40. Nice post. It is really interesting. Thanks for sharing the post!
    game pc

    ReplyDelete
  41. di tahun baru 2020 ini, kami selaku Agen Poker Online indonesia terbaik, menawarkan anda para penggemar poker online untuk bergabung. Kami menyediakan banyak sekali fasilitas permainan terlengkap dan terseru untuk para pecinta idn poker mania dengan minimal deposit idr 10 ribu saja, segera bergabung dan menangkan jackpot ratusan juta.

    ReplyDelete
  42. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.
    daftar game terbaik
    game pc
    info olahraga

    ReplyDelete
  43. Simply wanted to inform you not sure is it correct replacement Great efforts made here keep the effort
    Definitely a great post. I'm and software problems that you have people like me

    Login Indipoker
    Link Alternatif
    Link Alternatif Pokernet88
    Daftar Pokernet88
    Pokernet88
    Login Pokernet88
    Daftar Idn Poker

    ReplyDelete
  44. Pilihan terbaik dalam bermain judi poker online adalah memilih permainan pkv games online sehingga mudah dimainkan.
    https://isitup.org/partaiqq.online

    ReplyDelete
  45. QQSLOT777 เป็นเว็บการพนันออนไลน์ที่น่าเชื่อถือและดีที่สุดในประเทศไทย,ฝาก-ถอนขั้นต่ำเพียง 100 บาท.

    ReplyDelete
  46. Meja303 salah satu situs judi bola dan slot online terbaik, terpercaya dan paling aman di Indonesia. Minimal deposit 25000 dan banyak promo bonus tersedia untuk Anda semua member setia Meja303.

    situs judi slot
    slot online
    judi bola

    ReplyDelete
  47. Target4d bandar togel terpercaya minimal deposit 10000, 7 jenis pasaran terpopuler di Indonesia, gabung sekarang !

    bandar togel terpercaya
    promo bonus deposit togel

    ReplyDelete
  48. Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks.

    game android terbaik
    game pc terbaik

    ReplyDelete
  49. I have to thank you for the efforts you’ve IDN Poker put in writing this blog. I really hope to see the same high-grade content from you later on as well. In truth, your creative writing abilities has motivated me to get my very own blog now.

    ReplyDelete
  50. Jadi setiap member tidak perlu lagi merasakan khawatir untuk bermain di JanjiQQ dan akan di layani oleh CS profesional. Yang sudah ahli dalam bidangnya yang akan online selama 24 jam nonstop keaman dari pada Hacker.

    situs judi qq online terpercaya
    dominoqq
    dominoqq
    situs judi online

    ReplyDelete
  51. It’s a pity you don’t have a donate button! I’d certainly donate to situs idn poker online this excellent blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will talk about this site with my Facebook group. Chat soon!

    ReplyDelete
  52. ketika anda bermain dalam permainan bandarq dipastikan anda harus bermain dalam permainan poker online dimana dalam permainan ini modal kecil bisa mendapatkan kemenangan dengan mudah ketika bermain.

    ReplyDelete
  53. Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post!poker

    ReplyDelete
  54. ketika bermain dalam permainan dominoqq dipastikan anda harus bermain dalam permainan poker online dimana bisa menguntungkan anda ketika bermain.

    ReplyDelete
  55. bandarq Daftar sekarang juga serta dapatkan hadiah dan promo menarik di dalamnya http://www.google.com.na/url?q=http://94.46.114.7

    ReplyDelete
  56. Meraih kemenangan hingga puluhan dan ratusan juta rupiah??
    Tentu bisa, Berikut Bandar Judi terpercaya dan aman untuk di mainkan...
    BANDARQ
    Banyak agen judi online disana yang terpercaya, bettors hanya perlu memilih salah satunya yang dirasa merupakan cocok untuk bettors jadikan tempat bermain...
    http://www.google.com.gh/url?q=http://173.82.245.231
    http://www.google.ms/url?q=http://173.82.245.231

    ReplyDelete
  57. Kami menyediakan Pasaran dan Prediksi Bola Malam Ini Malam Ini setiap hari khusus untuk kamu, Member setia PAKARSCORE Hasil analisa tim spesialis Prediksi kami. Akurat Hasilnya, Buktikan Sendiri!

    Prediksi Bola Malam Ini

    ReplyDelete
  58. Thanks for many information i getting from your site newmacau88

    ReplyDelete