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

Saturday, July 28, 2012

Regular expressions - groups and alternations (Part 3)

This is part 3 in the series on regular expressions programming interview questions and answers. Here are the links to the previous posts:

  1. Regular expressions interview questions–Part 1
  2. Regular Expressions interview questions–part 2

In this post, we will talk about Groups. Groups in regular expressions allows you to perform different operations such as alternations, sub patterns, quantifiers, etc.

Question: Find all the occurrences of the, The and THE.

Here we will utilize the notion of groups and alternations. Alternation gives you a choice of alternate patterns to match. Let’s try a few solutions to this problem.

  1. The simplest solution is (the|The|THE). This matches any of the 3 different alternatives in the group.
  2. RegEx also has the notion of options. Options let you specify the way you would like to search for a pattern. For our example, we are interested in the ignore case option - (?i). Using this option, we can solve the above problem as (?i)the.
  3. A solution utilizing sub patterns can look like (tT)(hH)(eE).

Question: Find all the even numbers between 0 and 99.

The solution requires you to think and use two alternates – one for 0 to 9 set and the other for remaining using grouping. One possible solution is \b[24680]\b|\b[1-9][24680]\b.

Question: Identify hexadecimal numbers in a string of numbers.

This one is simple. [a-fA-F0-9].

Question: Ignore all vowels in the given text.

In this question, you need to use the negation operator ^. Note that the caret (^) at the beginning of the class means “No, I don’t want these characters.” (The caret must appear at the beginning.). Given this knowledge, the answer is quite simple – [^aeiou]

8 comments:

  1. Hi Nikhil,

    What a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this.
    I google so many articles and blog to know what exactly Proxy Pattern is and its examples. everywhere I say example related to banking or Images.
    But now I am confused and just want to know "Can we say that Server-side pagination comes under Proxy Pattern".
    From Blogs and Articles, I read, I come to know that proxy pattern create object having original object to interface its functionality to outer world. In other words, provide desire information from the real object as and when required.
    please correct me if I am wrong and help me to clear my confusion.
    But nice Article Mate! Great Information! Keep up the good work!

    Thanks a heaps,
    Sandy

    ReplyDelete
  2. Baltic Legal focuses on corporate law, tax law, business formation and registration and accounting services in all three Baltic States - Estonia, Latvia and Lithuania. If you would like to set up a company in the Baltic States, our professional team will provide you with comprehensive support and advice in the selection of the type of company, the establishment and registration of the company, the processing of tax issues and the promotion of your company in Latvia. Lithuania or Estonia.

    Because countries have different cultural, legal, and business systems, it can be difficult to start a business and register it overseas without local knowledge and experience. Our support guarantees the exact and timely implementation of all necessary measures to facilitate your deployment in the Baltic States. In addition, we can offer our clients comprehensive advice on non-business issues on many occasions. https://www.baltic-legal.com/business-support-services-eng.htm

    ReplyDelete
  3. Keep the balls rolling!! Nice posts you have given for us.
    brand strategy agency

    ReplyDelete
  4. Outstanding Blog! I want people to know just how good this information is in your Blog. I will visit your blog daily because I know. It may be very beneficial for me. If you want to Change Roadrunner Email Password please contact roadrunner support team for solution.

    ReplyDelete
  5. Regular expressions groups are a powerful tool for pattern matching. They allow you to capture and manipulate specific parts of a string. Nordvpn With Gaming This feature is invaluable for tasks like data extraction and validation.

    ReplyDelete
  6. They have a strong, large body and basic characteristics. Companion – Best sex dolls not only satisfies sexual desires but also makes a great companion. Older women can purchase sex dolls to keep them company.

    ReplyDelete