Skip to content

Instantly share code, notes, and snippets.

@devpruthvi
Created October 23, 2015 06:27
Show Gist options
  • Save devpruthvi/560d435c195c7df2fe8e to your computer and use it in GitHub Desktop.
Save devpruthvi/560d435c195c7df2fe8e to your computer and use it in GitHub Desktop.
A program for replacing nested "[yes]" patterns
public class Main {
public static String nestedReplace(String s) {
int nested =0,i=0;
StringBuilder o = new StringBuilder();
while(i<s.length())
{
if(nested==0 && s.regionMatches(i,"[yes]",0,4))
{
o.append("Yes");
i += 5;
continue;
}
else if(s.charAt(i) == '[')
nested++;
else if(s.charAt(i) == ']')
nested--;
o.append(s.charAt(i));
i++;
}
return new String(o);
}
public static void main(String[] args) {
System.out.println(nestedReplace("[yes] [no] [yes] [mk:[yes]] [yes] [iif:[yes] [no] [yes] iifnot [maybe]]"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment