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.

103 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. Yukk Gabung Bersama MileniaPoker - Poker Deposit Pulsa Dengan fitur - fitur lengkap serta Customer Service yang 24 jam. Poker Online

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

    Game Poker Online
    Poker Online CC
    Situs Poker Online Terpopuler
    MileniaPoker

    ReplyDelete
  11. 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
  12. 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
  13. Segera daftarkan diri anda sekarang juga di QQ1221 - Bandar Judi Online Terbesar dan Terpercaya di Indonesia.

    Link Alternatif QQ1221

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

    Bandar Judi Togel Online
    Togel Deposit Pulsa

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

    ReplyDelete
  16. 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
  17. 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
  18. 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
  19. pokerqq adalah permainan poker, dominoqq, bandarq online terbaik dengan deposit 15ribu.
    https://isitup.org/94.46.58.31

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

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

    ReplyDelete
  22. 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
  23. Nice post. It is really interesting. Thanks for sharing the post!
    game pc

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

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

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

    bandar togel terpercaya
    promo bonus deposit togel

    ReplyDelete
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. ketika bermain dalam permainan dominoqq dipastikan anda harus bermain dalam permainan poker online dimana bisa menguntungkan anda ketika bermain.

    ReplyDelete
  34. 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
  35. 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
  36. Thanks for many information i getting from your site newmacau88

    ReplyDelete
  37. http://202.95.10.94
    Maka untuk hal ini nantinya akan menjadi lebih mudah untuk situ juga nantinya termin untuk memasang transaksi deposit ini atau juga Tarik dana ini nantinya
    http://www.simulab.com.br/ead/blog/index.php?postid=58297

    ReplyDelete
  38. ketika anda bermain dalam permainan judi online dipastikan anda harus bermain dalam permainan bandarqq dimana anda bisa menadi yang terbaik dan luar biasa ketika bermain.

    ReplyDelete
  39. This amazing site, im really helpful after visit this site.

    newmacau88
    ion casino login

    ReplyDelete
  40. ketika bermain dalam permainan poker online dipastikan anda harus bermain dalam bandarqq dimana bisa menguntungkan anda ketika bermain.

    ReplyDelete
  41. kunjungi web saya mengenai berita bola terupdate, live score sepak bola, dan informasi pertandingan LIGA EROPA & INDONESIA terlengkap silahkan kunjungi web
    http://situsbola.id/

    ReplyDelete
  42. I've been checking for some superior to normal stuff concerning the issue and haven't had any karma up until this point, You just got another most key fan!.. pokerqq pkv games

    ReplyDelete
  43. Agen judi online terbaik di Indonesia dengan memberikan banyak bonus dan kemudahan dalam meraih kemenangan. Winrate yang diberikan dengan 85%!!!

    Bonus yang diberikan janjiqq adalah :
    - 0.3% untuk cashback yang akan didapatkan setiap minggunya!!!
    - 20% untuk bonus referral yang bisa didapatkan SEUMUR HIDUP!!!

    Tentunya banyak bonus yang diberikan serta mudahnya meraih kemenangan akan memberikan pengalaman yang tidak terlupakan bersama JANJIQQ. Tunggu apa lagi?

    Daftarkan diri anda sekarang juga untuk memenangkan uangnya sebanyak mungkin.
    - Domino99
    - BandarQ
    - Situs Poker Online

    ReplyDelete
  44. tentu saja tengah banyak sedang kompensasi http://188.114.238.15/ yang disediakan di website judi online paling baik yang ada di Indonesia ini http://wiznotes.com/UserProfile/tabid/84/userId/611044/Default.aspx

    ReplyDelete
  45. Bandar judi terbaik di Indonesia JANJIQQ dengan memberikan pelayanan 24 jam setiap hari!!!

    Dengan memberikan banyak bonus serta winrate 89% yang tinggi untuk semua member yang bermain!!!
    Bonus yang diberikan juga sangat menarik dengan :
    - 0.3% Bonus cashback Setiap minggunya
    - 20% Bonus referral yang didapatkan SEUMUR HIDUP!!!

    Menang berapapun pasti dibayar!!!
    Mainkan sekarang juga dan buktikan WINRATENYA!!!

    LANGSUNG DAFTAR JUGA SEKARANG BERSAMA KAMI :
    - Situs Poker Terpercaya
    - Dewa Poker
    - Situs Poker Online

    ReplyDelete
  46. slot online joker123 dengan freespin terbanyak hanya ada di bandar judi bola resmi. gabung bersama kami dan buktikan sendiri http://149.28.153.153/

    ReplyDelete
  47. The total population of Lithuania is 2,876,475 people. People in Lithuania speak the Lithuanian language. The linguistic diversity of Lithuania is diverse according to a fractionalization scale which for Lithuania is 0.3219. The median age is approximately 41.2 years. Life expectancy in Lithuania is 74. http://www.confiduss.com/en/jurisdictions/lithuania/demographics/

    ReplyDelete
  48. Temukan Info yang menarik dengan kunjungi situs ini !!!

    http://oxyforinternationalstudent.oxycreates.org/community/profile/infomenarik/
    http://winter-modular.com/community/profile/infomenarik/
    https://navigatorsusa.org/community/profile/infomenarik/
    https://ohaakademy.com/community/profile/infomenarik/
    https://labour.gov.bb/forum/profile/infomenarik/
    https://www.cmidonostia.es/es/community/profile/infomenarik/
    http://p-graph.org/community/profile/infomenarik/
    http://svtcomics.redstainmedia.com/community/profile/infomenarik
    https://ieltssampletest.com/international-ielts-discussion-forum/profile/infomenarik/
    http://www.lorishields.com/community/profile/infomenarik/
    https://ask.truelawyer.in/community/profile/infomenarik/
    https://amazingcareassist.com/community/profile/infomenarik/
    https://subtesty.pl/community/profile/infomenarik/
    https://conorneill.com/community/profile/infomenarik/
    https://raleigh-sb4059.com/community/profile/infomenarik/
    http://desanyitdah.zeta.co.id/community/profile/infomenarik/
    http://www.homesteadhow.com/community/profile/infomenarik/
    https://activitatsantcugat.com/community/profile/infomenarik/
    https://motoveeb.ee/community/profile/infomenarik/
    http://awcat.vfu.bg/community/profile/infomenarik/
    http://captainclever.com/rzz/community/profile/infomenarik/
    https://hestah.com/community/profile/infomenarik/
    https://tengear.com/community/profile/infomenarik/
    http://www.tsebhatu.com/community/profile/infomenarik/
    https://www.activeinthecommunity.org.uk/community/profile/infomenarik/

    ReplyDelete
  49. sudah terbiasa memberikan promo besar bosku dibandar togel online kingdomgrup http://128.199.187.54/

    ReplyDelete
  50. Artikel yang sangat menarik dan memberikan informasi yang bermaanfaat,
    semoga sukses selalu dan bisa terus berkarya
    Apabila ada yang ingin mau bermain togel online bisa banget nih gabung ke website Togel337. segeera join dan ambil keuntungannya !!
    bandar togel online
    togel hongkong
    keluaran HK
    pengeluaran HK
    keluaran HK pools
    data hk

    ReplyDelete
  51. https://128.199.82.247/
    https://128.199.82.247/

    ReplyDelete
  52. daftar segera jangan ragu bermain togel337 di sits terbaik kami! jangan ragu! raih kemenaganmu sekarang!

    pengeluaran hongkong
    data hk
    hk hari ini
    keluaran hk
    toto hk
    nomor hk

    ReplyDelete
  53. How to Change Your BellSouth Email Password?


    Today, it is imperative for email users to change their email passwords periodically to protect their data. Here is your step-by-step guide to change BellSouth password. First of all, visit the BellSouth login page. Next, log in to your BellSouth email account using your email credentials. Then, click on the My Profile option and then the 'set personal password' option. Now provide your current BellSouth email password. After this, create a new password and confirm it. At last, save changes.

    ReplyDelete
  54. berbagai macam jenis permainan game online tersedia disitus hkpools.

    ReplyDelete
  55. Terimakasih sudah memberikan Artikel yang sangat menarik dan bermanfaat.Saya sangat tertarik dengan apa yang diberikan didalam artikel tersebut dan semoga kedepannya bisa terus memberikan artikel yang menarik
    Semoga sukses selalu kaka

    Buat yang bari cari keluaran togel hongkong terbaru bisa banget nih kunjungi kesini
    pengeluaranhongkong.me
    pengeluaran hongkong
    pengeluaran hk

    Join dan ambil keuntungan yang berlimpah bosque !!



    ReplyDelete
  56. engan memahami proses dan cara bermain roulette online dengan benar. Itu akan memberikan sebuah peluang bagus dalam menghasilkan kemenangan besar. Disini ada sejumlah informasi tentang cara memulainya dengan baik dan benar. Dengan begitu akan memberikan harapan yang bagus nantinya.kunjungin situs kita di putih slot

    ReplyDelete
  57. when you and I look at the list of food below, and notice the fresh herbs and multitude of spices used in Indonesian cooking, it truly is a huge contributing factor as to why Indonesian cuisine is so flavorful and exciting to eat.
    Perusahaan Catering Di Indonesia

    ReplyDelete
  58. Mengutip dalam bermacam ragam tanggapan member dalam sudah melakukan gambling slot online rata-rata meraih bonus serasi ambisi pun pada luar asumsi. Tanggapan mereka membuat review terkemuka taruhan game slot online untuk selalu bersinar lalu membantu kehangatan melampaui keberhasilan bet semua member.

    ReplyDelete
  59. Bocoran Pola selanjutnya Cara tuk sanggup memenangi slot gacor pragmatic play tersebut yaitu melewati pola pakai segenap chip dalam datang. Di dalam untuk game slot online indonesia pragmatic play anda boleh memenuhi judi pakai chip, tidak semata-mata pakai bet pula. Lalu rata-rata chip terkait dapat kalian dapatkan dalam segenap pertaruhan pragmatic berbentuk kepingan minim pada dimana anda mampu dapatkan sebagian jenis kepingan.

    ReplyDelete
  60. 337Sports game slot online dinobatkan sebagai permainan judi Sbobet online paling banyak dimainkan oleh seluruh Indonesia. Banyak sekali alasan kenapa slot menjadi peminat bettor, selain cara main mudah, hadiah kemenangan juga sangat besar dan menguntungkan. Tidak ketinggalan jackpot slot yang bisa anda dapatkan, dijamin anda akan kaya raya jika main slot online di 337Sports.

    ReplyDelete