Matching Strings with Wildcard

Matching strings with wildcard

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; and add a reference to the Microsoft.VisualBasic.dll.

Since it's part of the .NET framework and will always be, it's not a problem to use this class.

How to match string with a wildcard in javascript

You can use regex.

const routes = ['\\/test\\/*', '\\/documentation\\/*']

function checkRoute(route) {
let regex = new RegExp(route);
let matchedRoutes = routes.filter(route => regex.test(route))
}

How to use wildcard in string matching

You are going to want to look at the re module. This will let you do a regular expression and accomplish the same thing as the * does in the linux command line.

How to match a wildcard for strings?

You are attempting to use wildcard syntax, but Groovy expects regular expression syntax for its pattern matching.

What went wrong with your attempt:

Attempt #1: p10.7.*

A regular expression of . matches any single character and .* matches 0 or more characters. This means:

p10{exactly one character of any kind here}7{zero or more characters of any
kind here}

You didn't realize it, but the . character in your first attempt was acting like a single-character wildcard too. This might match with p10x7abcdefg for example. It also does match p10.7.8 though. But be careful, it also matches p10.78, because the .* expression at the end of your pattern will happily match any sequence of characters, thus any and all characters following p10.7 are accepted.

Attempt #2: p10_7_*

_ matches only a literal underscore. But _* means to match zero or more underscores. It does not mean to match characters of any kind. So p10_7_* matches things like p10_7_______. Literally:

p10_7{zero or more underscores here}

What you can do instead:

You probably want a regular expression like p10_7_\d+

This will match things like p10_7_3 or p10_7_422. It works by matching the literal text p10_7_ followed by one or more digits where a digit is 0 through 9. \d matches any digit, and + means to match one or more of the preceding thing. Literally:

p10_7_{one or more digits here}

Compare Two Strings With Wildcards

Assuming that you mean * to be a single-character wildcard, the correct substitution in a Regex pattern is a dot (.):

string pattern = "He**o";
string regexPattern = pattern.Replace("*",".");

Regex.IsMatch("Hello",regexPattern); // true
Regex.IsMatch("He7)o",regexPattern); // true
Regex.IsMatch("he7)o",regexPattern); // false
Regex.IsMatch("he7)o",regexPattern, RegexOptions.IgnoreCase); // true

You might also want to anchor the pattern with ^ (start of string) and $ (end of string):

regexPattern = String.Format("^{0}$", pattern.Replace("*","."));

If you expect it to be able to parse input strings with special characters, you'll can escape all other characters like this:

string regexPattern = String.Join(".",pattern.Split("*".ToCharArray())
.Select(s => Regex.Escape(s)).ToArray());

String Matching with wildcard in Python

The idea is to convert what you are looking for, ABCDEF in this case, into the following regular expression:

([A]|\.)([B]|\.)([C]|\.)([D]|\.)([E]|\.)([F]|\.)

Each character is placed in [] in case it turns out to be a regex special character. The only complication is if one of the search characters is ^, as in ABCDEF^. The ^ character should just be escaped and is therefore handled specially.

Then you search the string for that pattern using re.search:

import re

substring = 'ABCDEF'
large_string = 'QQQQQABC.EF^QQQQQ'

new_substring = re.sub(r'([^^])', r'([\1]|\\.)', substring)
new_substring = re.sub(r'\^', r'(\\^|\\.)', new_substring)
print(new_substring)
regex = re.compile(new_substring)
m = regex.search(large_string)
if (m):
print(m.span())

Prints:

([A]|\.)([B]|\.)([C]|\.)([D]|\.)([E]|\.)([F]|\.)
(5, 11)

wildcard match & replace and/or multiple string wildcard matching

There are many ways to do this, but I came up with the following, which should work for your first question. Based on your examples I’m assuming you don’t want to match whitespace.

This function turns the first passed pattern into a regex and the passed replacement pattern into a string suitable for the re.sub function.

import re

def replaceWildcards(string, pattern, replacementPattern):
splitPattern = re.split(r'([*?])', pattern)
splitReplacement = re.split(r'([*?])', replacementPattern)
if (len(splitPattern) != len(splitReplacement)):
raise ValueError("Provided pattern wildcards do not match")
reg = ""
sub = ""
for idx, (regexPiece, replacementPiece) in enumerate(zip(splitPattern, splitReplacement)):
if regexPiece in ["*", "?"]:
if replacementPiece != regexPiece:
raise ValueError("Provided pattern wildcards do not match")
reg += f"(\\S{regexPiece if regexPiece == '*' else ''})" # Match anything but whitespace
sub += f"\\{idx + 1}" # Regex matches start at 1, not 0
else:
reg += f"({re.escape(regexPiece)})"
sub += f"{replacementPiece}"
return re.sub(reg, sub, string)

Sample output:

replaceWildcards("aaa.txt xx.txt.txt aaa.bat", "*.txt", "*.doc")
# 'aaa.doc xx.txt.doc aaa.bat'

replaceWildcards("aaa10.txt a1.txt aaa23.bat", "a??.txt", "b??.doc")
# 'aab10.doc a1.txt aaa23.bat'

replaceWildcards("aaa10.txt a1-suffix.txt aaa23.bat", "a*-suffix.txt", "b*-suffix.doc")
# 'aaa10.txt b1-suffix.doc aaa23.bat'

replaceWildcards("prefix-2aaa10-suffix.txt a1-suffix.txt", "prefix-*a*-suffix.txt", "prefix-*b*-suffix.doc")
# 'prefix-2aab10-suffix.doc a1-suffix.txt

Note f-strings require Python >=3.6.

Wildcard matching strings

The trick is to write a recursive function and match chars like for like until you find a '*'. Once '*' has been encountered then anything is fair game, so you can return true.

bool match(char const *pattern, char const *file) {
for (; *pattern != '\0'; ++pattern) {
switch (*pattern) {
case '*': {
//if pattern terminates after * then file can be anything, thus
//terminate and return true.
if (pattern[1] == '\0')
return true;
//pattern doesn't terminate so cut off '*' from pattern,
//increment file and repeat.
size_t max = strlen(file);
for (size_t i = 0; i < max; i++)
if (match(pattern + 1, file + i))
return true;
return false;
}
default:
//if pattern doesn't specify a '?' or a '*', it must be a regular
//character and so, must require a like for like match with file.
if (*file != *pattern)
return false;
++file;
}
}
//we have iterated through the whole of pattern and so file must end too
//if we are to match.
return *file == '\0';
}

You can then add extra branches to the switch statement and add functionality to your glob tool. For example, try adding the logic for a '?'.

Regular expression match string with wildcard

Try using this pattern:

val1/[^/]+/val3/.*

Sample:

String input = "val1/some_path/val3/stuff";
if (input.matches("val1/[^/]+/val3/.*")) {
System.out.println("match");
}


Related Topics



Leave a reply



Submit