Questions tagged [string-matching]

String matching is the problem of finding occurrences of one string (“pattern”, “needle”) in another (“text”, “haystack”).

There are two types of string matching:

  • Exact
  • Approximate

Exact string matching is the problem of finding occurrence(s) of a pattern string within another string or body of text. (NIST). For example, finding CGATCGATTA in CTAGATCCTGCGATCGATTAAGCCTGA.

A comprehensive online reference of string matching algorithms is Exact String Matching Algorithms by Christian Charras and Thierry Lecroq.

Approximate string matching, also called fuzzy string matching, searches for matches based on the edit distance between the pattern and the text.

2278 questions
7407
votes
3 answers

How to check whether a string contains a substring in JavaScript?

Usually I would expect a String.contains() method, but there doesn't seem to be one. What is a reasonable way to check for this?
gramm
  • 18,786
  • 7
  • 27
  • 27
2657
votes
36 answers

How do I check if a string contains a specific word?

Consider: $a = 'How are you?'; if ($a contains 'are') echo 'true'; Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?
Charles Yeung
  • 38,347
  • 30
  • 90
  • 130
675
votes
10 answers

What is the difference between re.search and re.match?

What is the difference between the search() and match() functions in the Python re module? I've read the Python 2 documentation (Python 3 documentation), but I never seem to remember it.
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
523
votes
10 answers

Check if string matches pattern

How do I check if a string matches the following pattern? Uppercase letter, number(s), uppercase letter, number(s)... Example: These would match: A1B2 B10L1 C1N200J1 These wouldn't ('^' points to problem) a1B2 ^ A10B ^ AB400 ^
DanielTA
  • 6,038
  • 3
  • 23
  • 27
234
votes
4 answers

PowerShell and the -contains operator

Consider the following snippet: "12-18" -Contains "-" You’d think this evaluates to true, but it doesn't. This will evaluate to false instead. I’m not sure why this happens, but it does. To avoid this, you can use this…
tnw
  • 13,521
  • 15
  • 70
  • 111
229
votes
12 answers

Return positions of a regex match() in Javascript?

Is there a way to retrieve the (starting) character positions inside a string of the results of a regex match() in Javascript?
stagas
  • 4,607
  • 3
  • 28
  • 28
191
votes
2 answers

High performance fuzzy string comparison in Python, use Levenshtein or difflib

I am doing clinical message normalization (spell check) in which I check each given word against 900,000 word medical dictionary. I am more concern about the time complexity/performance. I want to do fuzzy string comparison, but I'm not sure which…
Maggie
  • 5,923
  • 8
  • 41
  • 56
178
votes
12 answers

Javascript fuzzy search that makes sense

I'm looking for a fuzzy search JavaScript library to filter an array. I've tried using fuzzyset.js and fuse.js, but the results are terrible (there are demos you can try on the linked pages). After doing some reading on Levenshtein distance, it…
willlma
  • 7,353
  • 2
  • 30
  • 45
165
votes
28 answers

Find the Number of Occurrences of a Substring in a String

Why is the following algorithm not halting for me? In the code below, str is the string I am searching in, and findStr is the string occurrences of which I'm trying to find. String str = "helloslkhellodjladfjhello"; String findStr = "hello"; int…
bobcom
165
votes
9 answers

How to search a specific value in all tables (PostgreSQL)?

Is it possible to search every column of every table for a particular value in PostgreSQL? A similar question is available here for Oracle.
Sandro Munda
  • 39,921
  • 24
  • 98
  • 123
161
votes
25 answers

A better similarity ranking algorithm for variable length strings

I'm looking for a string similarity algorithm that yields better results on variable length strings than the ones that are usually suggested (levenshtein distance, soundex, etc). For example, Given string A: "Robert", Then string B: "Amy…
marzagao
  • 3,756
  • 4
  • 19
  • 14
134
votes
3 answers

Check whether a string contains a substring

How can I check whether a given string contains a certain substring, using Perl? More specifically, I want to see whether s1.domain.example is present in the given string variable.
Belgin Fish
  • 19,187
  • 41
  • 102
  • 131
116
votes
17 answers

Regular Expression Match to test for a valid year

Given a value I want to validate it to check if it is a valid year. My criteria is simple where the value should be an integer with 4 characters. I know this is not the best solution as it will not allow years before 1000 and will allow years such…
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
102
votes
7 answers

Filter multiple values on a string column in dplyr

I have a data.frame with character data in one of the columns. I would like to filter multiple options in the data.frame from the same column. Is there an easy way to do this that I'm missing? Example: data.frame name = dat days name 88 …
Tom O
  • 1,497
  • 3
  • 13
  • 16
85
votes
1 answer

How to check if matching text is found in a string in Lua?

I need to make a conditional that is true if a particular matching text is found at least once in a string of text, e.g.: str = "This is some text containing the word tiger." if string.match(str, "tiger") then print ("The word tiger was…
Village
  • 22,513
  • 46
  • 122
  • 163
1
2 3
99 100