Showing posts with label RegEx. Show all posts
Showing posts with label RegEx. Show all posts

Tuesday, May 9, 2017

Functional Programming - Find hidden string in the input

This is continuation of my functional programming experiments in JavaScript. This problem is from HackerRank.

The problem

Input is a string and we have to find out whether the letters of word 'hackerrank' are present inside it in the right order. When we say order, there might be some other letters in between the letters of 'hackerrank' the the order should be maintained. More details can be found in the actual problem link

Traditional solution

If the guy is not from the computer science background, there is 99% chance that the solution will be based on loops where a lot of states might have stored temporarily. But it looks like a familiar situation of email validation where we are expecting chaaracters in specific order.

The RegExp way

Regular expressions are great. There is no doubt on that. Here goes the regular expression way.

function checkForhackerrank(input) {
  if(testForhackerrank(input)) {
    return 'YES';
  }
  else return 'NO';
}
function testForhackerrank(input){
  return /h(.)*a(.)*c(.)*k(.)*e(.)*r(.)*r(.)*a(.)*n(.)*k/g.test(input)
}

Works great for the word hackerrank. Lets generalize

Generalized version

Below goes the generalized version which can work for any pattern other than 'hackerrank'.

function testForhackerrank(input){
  return getFindPattern('hackerrank').test(input);
}
function getFindPattern(stringToSearch) {
  return getGlobalRegExp(stringToSearch.split('').map(a=> `(.)*${a}`).join(''));
}
function getGlobalRegExp(pattern){
  return new RegExp(pattern,'g');
}

This doesn't need anything special regarding functional paradigm as the magic is done by regular expression. Plnkr is here.

Enjoy coding.

Monday, December 3, 2012

Regular expression - Replace exact word matched string

Regular expression are really helpful .There is no second thought on it. But as the fundamentals of regex is little difficult to digest for non-computer science graduates and most of the computer programmers are from non computer science background, it is very difficult to see regex in the production code. I don't know about the percentage of non computer graduates in programming in other countries, but in India its high. But there are exceptions as well, some of them spent time for learning these kind of things and applies in their programming life.

Anyway it is not the intention of this post to discuss about non computer science programmers using regex or not. This post will be listing out scenarios where a regular reducing a bulk amount of code

Find out exact word and replace

Lets consider the sentence "During blogging joymon's soul is filled with joy". We need to change the word 'joy' with 'happiness' .If we just use string.replace in .net it willl replace as "During blogging happinessmon's soul is filled with happiness".

         Dim  orgSentence = "During blogging joymon's soul is filled with joy" 
         Dim  replacedSentence = orgSentence.Replace("joy" , "happiness" )


Out put will be "During blogging happinessmon's soul is filled with happiness"

It did the task well but we didn't want 'joymon' to be replaced with "happinessmon".Unfortunately string.replace don't have flexibility to say replace only exact word matches. We can write a big function to achieve the same.But should we really need that as there are regular expression? Certainly not.We can achieve the same easily using regex.The code is as follows.

Dim  orgSentence = "During blogging joymon's soul is filled with joy" 
Dim  pattern As  String  = "\b"  + "joy"  + "\b" 
Dim  replaceWith = "happiness" 
Dim  reg As  Regex  = New  Regex (pattern)
Dim  replacedSentence As  String  = reg.Replace(orgSentence, replaceWith, 1, 0)
 

'\b' refers to word boundary.For more details on reg expressions visit the below links
http://msdn.microsoft.com/en-us/library/az24scfc.aspx#atomic_zerowidth_assertions
http://www.regular-expressions.info/wordboundaries.html

Monday, April 7, 2008

Online regular expression creator

Dont afraid of regular expressions any more...
Be friend with them..

http://gskinner.com/RegExr/ or www.regexr.com