tag:blogger.com,1999:blog-7440473.post8031999241424572330..comments2023-10-01T19:26:38.860+05:30Comments on Digizol: [Java] Validate opening & closing character pattern in textKamalhttp://www.blogger.com/profile/03241660835422874258noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-7440473.post-17321318003980897882014-01-30T12:12:06.513+05:302014-01-30T12:12:06.513+05:30This comment has been removed by a blog administrator.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7440473.post-62459911122168897572014-01-21T16:40:32.868+05:302014-01-21T16:40:32.868+05:30A small error is throw when controlling the matchi...A small error is throw when controlling the matching end pair and the stack is empty. The program must check if stack contains more elements.<br /><br /><br />...<br />} else if (closingChars.containsKey(ch)) {<br /> if (stack.isEmpty() || ch != stack.pop().getEnd()) {<br /> return false;<br />...<br />Bibihttps://www.blogger.com/profile/02360070657325377620noreply@blogger.comtag:blogger.com,1999:blog-7440473.post-48463328579650497832013-11-06T14:30:32.968+05:302013-11-06T14:30:32.968+05:30#1 Thanks for taking time on suggesting the improv...#1 Thanks for taking time on suggesting the improvement. However, the regexp is only looking for the availability of '(' at the start and ')' at the end, but nothing else.<br /><br />Effectively, "(<<<<])" String is also considered as well formatted based on this regular expression based program; which is actually not correct as per the requirement.Kamalhttps://www.blogger.com/profile/03241660835422874258noreply@blogger.comtag:blogger.com,1999:blog-7440473.post-15127972300540094542013-11-05T01:55:08.855+05:302013-11-05T01:55:08.855+05:30Simple and faster code:
import java.util.regex.Pa...Simple and faster code:<br /><br />import java.util.regex.Pattern;<br /><br />public class PatternValidator {<br /><br /> public static void main(String[] args) {<br /><br /> String[] samples = { "[(])", "([<>])", "([)", "()[]<>", "(()[]<>)",<br /> "(mal [ formatted )", "(this [ is < well > formatted ] text)" };<br /><br /> for (String each : samples)<br /> System.out.println(Pattern<br /> .compile("^\\(.*?\\)$", Pattern.CASE_INSENSITIVE)<br /> .matcher(each).find());<br /><br /> }<br /><br />}Anonymousnoreply@blogger.com