How to allow fulltext searching with hyphens in the search query

From here http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html One solution to find a word with a dashes or hyphens in is to use FULL TEXT SEARCH IN BOOLEAN MODE, and to enclose the word with the hyphen / dash in double quotes. Or from here http://bugs.mysql.com/bug.php?id=2095 There is another workaround. It was recently added to the manual: ” Modify a … Read more

Search File And Find Exact Match And Print Line?

Build lists of matched lines – several flavors: def lines_that_equal(line_to_match, fp): return [line for line in fp if line == line_to_match] def lines_that_contain(string, fp): return [line for line in fp if string in line] def lines_that_start_with(string, fp): return [line for line in fp if line.startswith(string)] def lines_that_end_with(string, fp): return [line for line in fp if … Read more

How to implement a Keyword Search in MySQL?

For a single keyword on VARCHAR fields you can use LIKE: SELECT id, category, location FROM table WHERE ( category LIKE ‘%keyword%’ OR location LIKE ‘%keyword%’ ) For a description you’re usually better adding a full text index and doing a Full-Text Search (MyISAM only): SELECT id, description FROM table WHERE MATCH (description) AGAINST(‘keyword1 keyword2’)

Show Count of Matches in Vim

Modern Vim Starting with Vim 8.1.1270, there’s a new feature in core to show the current match position. NeoVim enables this functionality by default, but standard Vim does not. To enable it in standard Vim, run: :set shortmess-=S Originally mentioned below in Ben’s answer, and added here for visibility. Older Versions In Vim 7.4+, the … Read more

Search a text file and print related lines in Python?

searchfile = open(“file.txt”, “r”) for line in searchfile: if “searchphrase” in line: print line searchfile.close() To print out multiple lines (in a simple way) f = open(“file.txt”, “r”) searchlines = f.readlines() f.close() for i, line in enumerate(searchlines): if “searchphrase” in line: for l in searchlines[i:i+3]: print l, print The comma in print l, prevents extra … Read more

Regex for PascalCased words (aka camelCased with leading uppercase letter)

([A-Z][a-z0-9]+)+ Assuming English. Use appropriate character classes if you want it internationalizable. This will match words such as “This”. If you want to only match words with at least two capitals, just use ([A-Z][a-z0-9]+){2,} UPDATE: As I mentioned in a comment, a better version is: [A-Z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]* It matches strings that start with an uppercase letter, … Read more

Laravel – search with multiple keywords against multiple columns with the search result to be ordered in relevance

I’m not sure whether this has another method. But this is what I thought of it $word1 = ‘word1’; $word2 = ‘word2’; $word3 = ‘word3’; $all = DB::table(‘posts’) ->where(‘meta_name’, ‘like’, “%{$word1}%”) ->where(‘meta_name’, ‘like’, “%{$word2}%”) ->where(‘meta_name’, ‘like’, “%{$word3}%”) ->orWhere(function($query) use ($word1, $word2, $word3) { $query->where(‘meta_description’, ‘like’, “%{$word1}%”) ->where(‘meta_description’, ‘like’, “%{$word2}%”) ->where(‘meta_description’, ‘like’, “%{$word3}%”); }); $twoWords = … Read more