Matching strings with wildcard

wildcard pattern matching recursive
wildcard-pattern matching coding blocks
wildcard matching leetcode python
python wildcard search in list
string matching algorithm geeksforgeeks
python wildcard in if statement
regular expression match interviewbit solution
wildcard in c

I would like to match strings with a wildcard (*), where the wildcard means "any". For example:

*X = string must end with X
X* = string must start with X
*X* = string must contain X

Also, some compound uses such as:

*X*YZ* = string contains X and contains YZ
X*YZ*P = string starts with X, contains YZ and ends with P.

Is there a simple algorithm to do this? I'm unsure about using regex (though it is a possibility).

To clarify, the users will type in the above to a filter box (as simple a filter as possible), I don't want them to have to write regular expressions themselves. So something I can easily transform from the above notation would be good.

Just FYI, you could use the VB.NET Like-Operator:

string text = "x is not the same as X and yz not the same as YZ";
bool contains = LikeOperator.LikeString(text,"*X*YZ*", Microsoft.VisualBasic.CompareMethod.Binary);  

Use CompareMethod.Text if you want to ignore the case.

You need to add using Microsoft.VisualBasic.CompilerServices;.

Wildcard Pattern Matching, '*' character matches with one or more characters in Text. Here we will move to next character in the string. Case 2: The character is '?' We can ignore current  Commonly used wildcard characters are the asterisk (*). It represents zero or more characters in a string of characters. In the following example asterisk is used to match words that begins with m and ends with e −

Often, wild cards operate with two type of jokers:

  ? - any character  (one and only one)
  * - any characters (zero or more)

so you can easily convert these rules into appropriate regular expression:

  // If you want to implement both "*" and "?"
  private static String WildCardToRegular(String value) {
    return "^" + Regex.Escape(value).Replace("\\?", ".").Replace("\\*", ".*") + "$"; 
  }

  // If you want to implement "*" only
  private static String WildCardToRegular(String value) {
    return "^" + Regex.Escape(value).Replace("\\*", ".*") + "$"; 
  }

And then you can use Regex as usual:

  String test = "Some Data X";

  Boolean endsWithEx = Regex.IsMatch(test, WildCardToRegular("*X"));
  Boolean startsWithS = Regex.IsMatch(test, WildCardToRegular("S*"));
  Boolean containsD = Regex.IsMatch(test, WildCardToRegular("*D*"));

  // Starts with S, ends with X, contains "me" and "a" (in that order) 
  Boolean complex = Regex.IsMatch(test, WildCardToRegular("S*me*a*X"));

Wildcard Matching, Given an input string ( s ) and a pattern ( p ), implement wildcard pattern matching with support for '?' and '*' . '?' Matches any single character. '*' Matches any  String matching where one string contains wildcard characters. Given two strings where first string may contain wild card characters and second string is a normal string. Write a function that returns true if the two strings match. The following are allowed wild card characters in first string. * --> Matches with 0 or more instances of any character or set of characters. ? --> Matches with any one character.

Using of WildcardPattern from System.Management.Automation may be an option.

pattern = new WildcardPattern(patternString);
pattern.IsMatch(stringToMatch);

Visual Studio UI may not allow you to add System.Management.Automation assembly to References of your project. Feel free to add it manually, as described here.

Wildcard Pattern Matching, Wildcard Pattern Matching: Given a string and a pattern containing wildcard characters i.e. '*' and '?', where '?' can match to any single character in input string  I would like to match strings with a wildcard (*), where the wildcard means "any". For example: *X = string must end with X X* = string must start with X *X* = string must contain X Also, some compound uses such as: *X*YZ* = string contains X and contains YZ X*YZ*P = string starts with X, contains YZ and ends with P.

*X*YZ* = string contains X and contains YZ

@".*X.*YZ"

X*YZ*P = string starts with X, contains YZ and ends with P.

@"^X.*YZ.*P$"

Matching wildcards, Find Complete Code at GeeksforGeeks Article: http://www.geeksforgeeks.org/​wildcard Duration: 4:51 Posted: Aug 8, 2017 Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z.

A wildcard * can be translated as .* or .*? regex pattern.

You might need to use a singleline mode to match newline symbols, and in this case, you can use (?s) as part of the regex pattern.

You can set it for the whole or part of the pattern:

X* = > @"X(?s:.*)"
*X = > @"(?s:.*)X"
*X* = > @"(?s).*X.*"
*X*YZ* = > @"(?s).*X.*YZ.*"
X*YZ*P = > @"(?s:X.*YZ.*P)"

String matching where one string contains wildcard , Assuming that you mean * to be a single-character wildcard, the correct substitution in a Regex pattern is a dot ( . ): string pattern = "He**o";  To get the value of the first match in a range using a wildcard, you can use an INDEX and MATCH formula, configured for exact match. In the example shown, the formula in F5 is: = INDEX ( B5:D5 , MATCH ( E5 & "*" , B5:D5 , 0 ))

Compare Two Strings With Wildcards, Regex, or regular expressions, is a method to search for strings which match a pattern. To match wildcard strings, replace single wildcard characters with . and  A wildcard set can include both single characters and ranges. The following example uses the [] operator to find a string that begins with a number or a series of special characters. SELECT [object_id], OBJECT_NAME(object_id) AS [object_name], name, column_id FROM sys.columns WHERE name LIKE '[0-9!@#$.,;_]%'; Here is the result set.

How to search strings with wildcard in Python, one main string and another wildcard patterns are given. In this algorithm, it will check whether the wildcard pattern is matching with the ma How can you use wildcards in Excel's IF Function - with this method you can easily check for a partial text match. It's a clever method to get the IF function to check if the cell value contains a word.

Wildcard Pattern Matching, Commonly used wildcard characters are the asterisk (*). It represents zero or more characters in a string of characters.In the following example  Pandas: Select rows that match a string less than 1 minute read Micro tutorial: Select rows of a Pandas DataFrame that match a (partial) string. import pandas as pd #create sample data data = {'model': ['Lisa', 'Lisa 2', 'Macintosh 128K', 'Macintosh 512K'], 'launched': [1983, 1984, 1984, 1984], 'discontinued': [1986, 1985, 1984, 1986]} df = pd.

Comments
  • Should YZ ABC X match *X*YZ*, i.e. do the substrings need to appear in the same order in both the string and the pattern or not? I'd assume it shouldn't match, but "string contains X and contains YZ" doesn't make it clear. If it should match, all the current answers are wrong.
  • That would be a no. In the example given, X must appear before YZ.
  • hmm, adding "using" results in:Type or namespace name 'CompilerServices' does not exist in namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?
  • You need to add a reference to the Microsoft.VisualBasic.dll: stackoverflow.com/a/21212268/284240
  • It appears that this is no longer available in .Net 4.6. :(
  • @AndrewRondeau Are you sure? May have to update the correct answer in that case, i.e. I'm guessing right now it's a bug waiting to happen for me.
  • I'm using 4.7 and it works fine. There is a note on the website saying it's not supported in .NET Core and .NET Standard projects though.
  • Great solution!
  • It's not as easy as you claim. For example, one specialty is that when using Directory.GetFiles, a three letter extension .htm would also match .html, but a two letter extension .ai would not match aix or aifg. Windows wildcards are trivial on first sight, but under the hood, they're a bunch of grown legacy hypercomplex rulesets.
  • @Sebastian Mach: Thank you for mentioning the nuance! I agree that MS DOS (and Windows) interpretation of the wild cards is different from standard one en.wikipedia.org/wiki/Wildcard_character However, the question is about strings and it doesn't mention files; that's why I've put the simplest solution assuming * being any characters (zero or more) and ? being exactly one character .
  • The original question was for string identifiers, not the filesystem, correct.
  • If you worry about performance, here's a C# implementation of a wildcard matching algorithm which is a lot faster than RegEx for this specific problem.
  • OK so with regex there's nothing I can simply replace * with to get what I want? These queries are going to be run by users and I don't expect them to understand regular expressions.
  • Yep, but start and end you need to specify anchors. ^ start, $ end