Regex from Basics


  1. To match a specific string say tutsonly, the regex for this is "tutsonly" without quotes.
  2. To match any character except newline use a dot ( . )
  3. i.e for xxx.xxx.xxx where x can b any character use ...\\....\\....\\. as regex code. where \\. is used for dot and ( . ) for any character.
  4. The expression \d matches any digit from [0-9]
  5. The expression \D matches any character that is not digit.
  6. The expression \s matches any white space character.
  7. The expression \S matches any non-white space character.
  8. The expression \w will match any word character. Word character includes alphanumeric characters (a-z, A-Z and 0-9) and underscores (_)
  9. The expression \W will match any non-word character. Word character includes alphanumeric characters (a-z, A-Z and 0-9) and underscores (_) i.e $
  10. The ^ symbol matches the position at the start of a string. i.e ^123 in 1234123
  11. The $ symbol matches the position at the end of a string. i.e 123$ in 1234123
  12. The character class [ ] matches only one out of several characters placed inside the square brackets.
  13. The negated character class [^] matches any character that is not in the square brackets.
  14. A hyphen inside a character class specifies a range of characters. For example: [a-z] or [A-Z] or [0-9]
  15. The tool {x} will match exactly  repetitions of character/character class/groups.
  16. The {x,y} tool will match between  and  (both inclusive) repetitions of character/character class/group.
  17. The * tool will match zero or more repetitions of character/character class/group.
  18. The + tool will match one or more repetitions of character/character class/group.
  19. The $ boundary matcher matches an occurrence of a character/character class/group at the end of a line.
  20. \b assert position at a word boundary.
    Three different positions qualify for word boundaries :
    ► Before the first character in the string, if the first character is a word character.
    ► Between two characters in the string, where one is a word character and the other is not a word character.
    ► After the last character in the string, if the last character is a word character.
  21. Parenthesis ( ) around a regular expression can group that part of regex together. This allows us to apply different quantifiers to that group.
  22. Alternations ( | ), matches single regular expression out of several possible regexes. It matches either everything to the left or everything to the right of the vertical bar. We need to use parenthesis to limit the use of alternations.
  23. The positive lookahead (?=) asserts regex_1 to be immediately followed by regex_2. The lookahead is excluded from the match. It does not return matches of regex_2. The lookahead only asserts whether a match is possible or not.
  24. The negative lookahead (?!) asserts regex_1 not to be immediately followed by regex_2. Lookahead is excluded from the match (do not consume matches of regex_2), but only assert whether a match is possible or not.
  25. The positive lookbehind (?<=) asserts regex_1 to be immediately preceded by regex_2. Lookbehind is excluded from the match (do not consume matches of regex_2), but only assert whether a match is possible or not.
  26. The negative lookbehind (?<!) asserts regex_1 not to be immediately preceded by regex_2. Lookbehind is excluded from the match (do not consume matches of regex_2), but only assert whether a match is possible or not.
Answer : (^\\d)\\w\\w\\w\\w(\\.$)


2. The character class [ ] matches only one out of several characters placed inside the square brackets.

You have a test string .
Your task is to write a regex that will match  with following conditions:
 must be of length: 6
First character: 12 or 3
Second character: 12 or 0
Third character: xs or 0
Fourth character: 30 , A or a
Fifth character: xs or u
Sixth character: . or ,

Answer : ^[123][120][xs0][30Aa][xsu][\\.,]$

2. The negated character class [^] matches any character that is not in the square brackets.
E.g. You have a test string 
Your task is to write a regex that will match  with the following conditions:

must be of length 6.
First character should not be a digit (  or  ).
Second character should not be a lowercase vowel (  or  ).
Third character should not be bcD or F.
Fourth character should not be a whitespace character ( \r, \n, \t, \f or <space> ).
Fifth character should not be a uppercase vowel (  or  ).
Sixth character should not be a . or , symbol.

Answer: ^[^\\d][^aeiou][^bcDF][^\\r\\n\\t\\f][^AEIOU][^\\.,]$


Your task is to write a regex that will match  using the following conditions:

  •  must be of length, greater than or equal to 5.
  • First character should be a lowercase alphabet.
  • Second character should be a positive digit.
  • Third character should not be a lowercase alphabet.
  • Fourth character should not be a uppercase alphabet.
  • Fifth character should be an uppercase alphabet.
Answer: ^[a-z][1-9][^a-z][^A-Z][A-Z]

( | ), matches single regular expression out of several possible regexes. It matches either everything to the left or everything to the right of the vertical bar. We need to use parenthesis to limit the use of alternations.

Comments

  1. Where to buy titanium trim | TITanium Art | TITanium Art
    Titsanium Art offers a good design idea of all silicone dab rig with titanium nail things but also has a great visual appeal 2020 ford ecosport titanium for titanium knee replacement titanium engagement rings for her in titanium damascus knives titanium, to be honest.

    ReplyDelete

Post a Comment

Popular posts from this blog

Java Practice Questions MCQ

Linux Assignments - Practise

HackerRank Introduction Challenges - Java