This is a great question and a good example of how we can use character lists and regular expression boundaries to design queries and get the data we want.
Depending on the accuracy we can wish for and the data that we have in the database, we can certainly develop custom queries based on various expressions, such as this example for New York State with various types:
([new]+\s+[york]+\s+[stae]+)
Here we have three lists of characters that we can update with other possible letters.
[new] [york] [stae]
We also added two sets of \s+ as our borders here to increase accuracy.
This snippet shows how capture groups work:
const regex = /([new]+\s+[york]+\s+[stae]+)/gmi; const str = 'Anything we wish to have before followed by a New York Statse then anything we wish to have after. Anything we wish to have before followed by a New Yokr State then anything we wish to have after. Anything we wish to have before followed by a New Yokr Stats then anything we wish to have after. Anything we wish to have before followed by a New York Statse then anything we wish to have after. '; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the 'm'-variable. m.forEach((match, groupIndex) => { console.log('Found match, group ${groupIndex}: ${match}'); }); }
Php
$re = '/([new]+\s+[york]+\s+[stae]+)/mi'; $str = 'Anything we wish to have before followed by a New York Statse then anything we wish to have after. Anything we wish to have before followed by a New Yokr State then anything we wish to have after. Anything we wish to have before followed by a New Yokr Stats then anything we wish to have after. Anything we wish to have before followed by a New York Statse then anything we wish to have after. '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
source share