32672

The recursive method which will determine whether or not two strings match. The matching process sho

Question:

the recursive method match(String x, String y) in the code below which will determine whether or not two strings match. The matching process should allow "wild cards". A '@' character will match with any other single character and a '*' character will match with 0 or more characters of any type.

So far this is what i got:

public static boolean match(String x, String y) { int len = x.length() < y.length() ? x.length() : y.length(); if (len == 0 && x.length() > 0) return false; else if (len == 0 && y.length() > 0) return true; for (int i = 0; i < len;) { int v1 = x.charAt(i); int v2 = y.charAt(i); if (v1 == v2) return match(x.substring(1, x.length()), y.substring(1, y.length())); else return false; } return false; }

Any help or direction will be highly appreciated.. More specifically how could i implement the Wildcard thing using recursion.

Answer1:

Take a look at this. A first idea, could be improved.

public static boolean match(String regex, String input) { return match(regex.toCharArray(), input.toCharArray(), regex.length() - 1, input.length() - 1); } private static boolean match(char[] regex, char[] input, int regexPosition, int inputPosition) { if (regexPosition < 0 || inputPosition < 0) { return false; } if (regexPosition == 0 && inputPosition == 0) { return true; } if (regex[regexPosition] == input[inputPosition] || regex[regexPosition] == '@') { return match(regex, input, regexPosition - 1, inputPosition - 1); } if (regex[regexPosition] == '*') { if (regex[regexPosition - 1] == '@') { /* @* => @ matter of taste. Sure there are counter examples. */ return match(regex, input, regexPosition - 2, inputPosition - 1); } final int newInputPosition = String.valueOf(input).lastIndexOf(regex[regexPosition - 1]); if (newInputPosition >= 0) { return match(regex, input, regexPosition - 1, newInputPosition); } } return false; }

Recommend

  • How can I count unique terms in a plaintext file case-insensitively?
  • Programmatically Update Linked Named Range of excel object in MS Word (2007)
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • Remove final comma from string in vb.net
  • copying resource to sdcard gives a damaged file in android
  • PHP - How to update data to MySQL when click a radio button
  • AES padding and writing the ciphertext to a disk file
  • sending/ receiving email in Java
  • VBA Convert delimiter text file to Excel
  • ORA-29908: missing primary invocation for ancillary operator
  • retrieve vertices with no linked edge in arangodb
  • How to disable jQuery.jplayer autoplay?
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • Cant find why the layout is getting smaller
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?