Regular expressions interview questions and answers

1)What is Regular expressions? 

-Regular expressions is used to perform powerful and efficient text processing

-It is simple like search editors and powerful text processing language

-This is powerful pattern language

-Matching performed based on the bit pattern used for the encoding the character not based graphic representation of the character

-Regualr expressions composed with two types of characters 

1)meta characters (like *)  

2)litrals 

2)How many types of Regex engines available ?

two types 

1)DFA

2)NFA

3)What are good regex concerns (or) principles? 

1)Understanding the requirement properly 

2)Matching what you want 

3)Keeping the Regex manageable and understandable 

4)What is Anchors in Regular expressions?


Anchors not matching anything but pointing the positions in given data

There are two types of anchors 

1) Simple ones  (^ , $,\G ...etc)
2)Complex ones (lookbehind and lookahead)

5)What is the difference between ^ and \A in Regular expressions?

^ - Matches the starting of the line. Effecting the multiline mode
Ex: ^Bob
Bob is good
Bob is Nice

-> if used this in character class working as a negate character.
[^abc] - matching anything except a,b,c

\A -  Matches the starting of the line. Not effecting the multiline mode.
Ex: \ABob - Bob is good.

6)What is the difference between \Z and $ in Regular expressions?

\Z -  Matches the end of the line. Not effecting the multiline mode.
ExShiva\Z
Gummadidal Shiva
Regex classes by Gummadidal Shiva

$ - Matches the end of the line.effecting the multiline mode.
Ex: Shiva$
Gummadidal Shiva
Regex classes by Shiva. Shiva is good teacher
Thanks to Shiva

[[:upper:]] - Matches capital letters is equal to [A-Z].
Ex: GUMMADIDALA shiva From Hyderabad,India

7)What is Character class in Regular expressions?

Character  class always match one character.
[abc]-alwaya match  either a or b or c.

8)What is ? (Or) optional character in Regular expressions?

Optional character is used to make charecter as optional, if character exist regex will match , if not ignoring the character
July?- is matching July or jul

9)What is the use of parentheses in Regular expressions?

Using parentheses perform two kind of functions 
1) grouping
2) capturing

10)What is Greedy quantifiers * ,+,?     ?.

Greedy quantifiers trying to match as much as possible.

11)What is  Lazy quantifiers *? ,+?,??     ?.

Non Greedy or lazy quantifiers trying to match as possible minimum to satisfy condition

12)How do you see all text files from your system?

Go to search type *.txt 

meaning of pattern is to select all the files which has extension .txt

13)What is Backreferencing in Regular expressions? 

Backreferencing is a feature of regular expressions, it is used to match new text that is same as matched earlier in the expression.

[0-9]+  match all the digits 9334

([0-9])\1+   match only 9334

\1 represent the match which was matched ([0-9]) 

14)What is lookaround in Regular expressions?

Generally regex works left to right but sometimes needs to process the text right to left.

Processing text right to left is called lookaround.

example : Needs to insert coma after three digits in cash 

12,345,678

15)How lookaround works in Regular expressions?

It is working like meta characters [\b][^][$] but they match the text based on positions with in the text.

16)Types of lookaround in Regular expressions?

we have two types of look around.

1) lookahead

2)lookbehind

17)What is lookahead in Regular expressions?

It is working towords to the right to see if match is exist 

Positive Lookahead:

If successful match to the right 

positive lookahead is specified with ( ?=...)

Ex: (?=\d)  successful where digit comes right 

Negative Lookahead:

If successful not match to the right 

positive lookahead is specified with ( ?!=...)

18)What is lookbehind in Regular expressions?

It is working towords to the left to see if match is exist 

Positive Lookbehind:

If match is successful match to the left

Positive lookbehind is specified with ( ?<=...)

Ex: (?<=\d)  successful where digit comes left

Negative Lookbehind:

If match is successful not match to the left

Negative lookbehind is specified with ( ?<!...)

19)What is the meaning "(?" in Regula expressions?

Open parenthesis sequence , They all begin with two characters like (? always perform some special functionality 

(?: ..) : group it but don't capture 

(?=...): look ahead 

(?<=...) : look behind

20)What is meta character represent the space in Regular expressions?

\s - is used represent the space 

\s* - represent more than one whitespace.

Shiva\s*Gummadidala 

Shiva   Gummadidala 

21) What is escape sequence character in Regex?

'\' character represent escape sequence.

Want to match any character which is having special meaning then we use escape sequence character.

a\^b will match a^b

22) What is regular expression flags ?

Ragular expression flags sometimes called as modifiers , they are effect the way of pattren matching.

Different kinds of flags available

/g,/i etc. 

23)What is meta character in Regular expressions?

Meta characters used to form syntax of regular expressions.

Ex: ^, .,& etc..

24) Can we give regular expression range in descending order like [20-10] ?

No , we shouldn't give , if you give will out of range expression error.

25)What is CapturingGroup Regular expressions?

-if you feel portion of the regex using later part of the regex then we use capturing groups.

-its represent with meta characters inside parenthesis and its repetitions represented with  \1 , \2 etc..

- its also called as Backreferencing

   (abcd)(efgh) \1\2 will match abcdefghabcdefgh 

26)What is NonCapturingGroup Regular expressions?

if you want to make capturing group as non NonCapturingGroup  use ?:.

(?:abcd) is NonCapturingGroup - can not use this group later.

27) What is the difference between ([PQR]+) and  ([PQR])+?

Both are matching PQR , 

-([PQR]+)  put PQR saved in first back reference.

 -([PQR])+ , first time P was saved , second time Q was saved and third time R was saved. + causes the pair of parenthesis to repeat three times. 

28) What is the namespace represented the Regular expressions in C#.NET?

System.Text.RegularExpressions.Regex.

29) What is character class subtraction in Regular expressions?

Want to exclude set of character from matched text then use character class subtraction.

syntax :  [Base_group -[Exclude_group]]

[0-5-[23]]

given input data array is  {"12","145",""1054}

output is array is  {"145",""1054} 2 excluded so that "12" not getting selected.

30) What is Name matched sub expressions in Regular expressions?

Matched string captured by sub expression by using name. Let you access this it by name  or number

syntax : (?<givne_name>  subexpression)

Ex : (?<Given_Name> \w+)   -> Capturing method syntax 

Console.WriteLine(Match.Goups["given_name"].value);

31)What is conditional matching in Regex?

-Using conditional matching  match the text from one of two given patterns.

(?(capturinggroup) pattern1/pattern2)

((?<n2>\d{2}-)\b\d{2}\b | \b\d{4}\b)

12-222-9999

32)What is Zero width assertions in Regular expressions?  

These are using for matching the position rather than text the line.

\A - Matching the starting of  the line.

\Z - Matching the end of the line. Etc


For Regular expressions core concepts click here

Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

No comments:

Post a Comment