wildcard search using replace function

user_007

I tried to implement wildcards search in my application using some algorithms (k-gram algorithm etc.), but it was very complex.

Until i found this code, and it works perfectly .. but i don't know how it get check and get results !

Code:

public static boolean wildCardMatch(String text, String pattern)
{
  return text.matches( pattern.replace("?", ".?").replace("*", ".*?") );
}

Does their anyone help me to knows how it work ? what's the idea of replace function?

Olivier Grégoire

What you're speaking about is called the glob pattern.

In the Java world, the glob pattern is more often translated into a regex pattern.

In your scenario, the implementation is very basic: the replace method is used to replace all occurrences of ? into the regex equivalent .?. Then all occurences of * are replaced by .*?.

So if you have the following glob pattern: abc*.def, the regex will become abc.*?.def.

When the regex is finally ready, it is used to be checked against the variable text using the method matches. This latter method accepts a regex as input.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursive wildcard search and replace

From Dev

vim swap substrings while using a wildcard in search and replace

From Dev

javascript wildcard replace function

From Dev

Wildcard (*,!) on search using Regex

From Dev

String wildcard in pandas on replace function

From Dev

Excel Function, Search for Max wildcard (*)

From Dev

Excel - IF function using a wildcard

From Dev

Notepad++ Wildcard problems in search and replace

From Dev

CodeIgniter "like()" function with % wildcard inside search terms

From Dev

CodeIgniter "like()" function with % wildcard inside search terms

From Dev

Using wildcard characters in SUM function

From Dev

Replacing comma by dot only in numbers in a texteditor using search and replace function

From Dev

Python - Using str.replace with a wildcard

From Dev

Elasticsearch query_string search using wildcard

From Dev

How to search terms with wildcard in elasticsearch using querystringquery

From Dev

Search by hash character using wildcard in elasticsearch

From Dev

How can I Find/Replace part of a wildcard search in Notepad++

From Dev

How can I replace around a wildcard in a regex search?

From Dev

Search and replace using awk

From Dev

search and replace using python

From Dev

Regular Expressions || How to get a "wildcard" in, specifically replace() function

From Dev

python replace string function throws asterix wildcard error

From Dev

ElasticSearch with Nest: Partial search using multiple words using Query<>.Wildcard

From Dev

find and replace "tabs" using search and replace in nano

From Dev

Using regex in the "replace" part of a search/replace?

From Dev

Backward search and replace using sed

From Dev

search and replace using grep (not sed)

From Dev

Search and replace using prename or sed

From Dev

Find and replace using wildcard/regex characters in Notepad++

Related Related

  1. 1

    Recursive wildcard search and replace

  2. 2

    vim swap substrings while using a wildcard in search and replace

  3. 3

    javascript wildcard replace function

  4. 4

    Wildcard (*,!) on search using Regex

  5. 5

    String wildcard in pandas on replace function

  6. 6

    Excel Function, Search for Max wildcard (*)

  7. 7

    Excel - IF function using a wildcard

  8. 8

    Notepad++ Wildcard problems in search and replace

  9. 9

    CodeIgniter "like()" function with % wildcard inside search terms

  10. 10

    CodeIgniter "like()" function with % wildcard inside search terms

  11. 11

    Using wildcard characters in SUM function

  12. 12

    Replacing comma by dot only in numbers in a texteditor using search and replace function

  13. 13

    Python - Using str.replace with a wildcard

  14. 14

    Elasticsearch query_string search using wildcard

  15. 15

    How to search terms with wildcard in elasticsearch using querystringquery

  16. 16

    Search by hash character using wildcard in elasticsearch

  17. 17

    How can I Find/Replace part of a wildcard search in Notepad++

  18. 18

    How can I replace around a wildcard in a regex search?

  19. 19

    Search and replace using awk

  20. 20

    search and replace using python

  21. 21

    Regular Expressions || How to get a "wildcard" in, specifically replace() function

  22. 22

    python replace string function throws asterix wildcard error

  23. 23

    ElasticSearch with Nest: Partial search using multiple words using Query<>.Wildcard

  24. 24

    find and replace "tabs" using search and replace in nano

  25. 25

    Using regex in the "replace" part of a search/replace?

  26. 26

    Backward search and replace using sed

  27. 27

    search and replace using grep (not sed)

  28. 28

    Search and replace using prename or sed

  29. 29

    Find and replace using wildcard/regex characters in Notepad++

HotTag

Archive