How do I filter by file type on Sourcegraph? - sourcegraph

I want to do a Sourcegraph search and have it show only results from files with a certain extension.

You can use the file: token to filter results by filename regex. For example, file:\.go$ would limit results to files ending in ".go".
The full search query syntax is documented here: https://docs.sourcegraph.com/code_search/reference/queries

Related

Extract third level of folder structure stored in MySQL field

I'm using the following regex (https://regex101.com/r/Kt9sNj/1) in PHP to get all the files in the third level of a directory:
/^(\/[^\/]*){1,4}\/?$/m
Then if I have the following data:
/home/myuser/folder_example/first_file.txt
/home/myuser/folder_example/second_file.txt
/home/myuser/folder_example/third_file.txt
I get:
first_file.txt
second_file.txt
third_file.txt
I try to use this in a MySQL query that contains an array of a json object.
My Query is:
SELECT data->'$.files' AS File
FROM table
WHERE user = 'myuser';
And I get:
["/home/myuser/folder_example/first_file.txt","/home/myuser/folder_example/second_file.txt","/home/myuser/folder_example/third_file.txt"]
But when I use that regex on my sql query:
SELECT data->'$.files' AS File
FROM table
WHERE user = 'myuser'
AND data->'$.files' REGEXP '^(\/[^\/]*){1,4}\/?$';
I need to get this (all files under that directory):
["first_file.txt","second_file.txt","third_file.txt"]
It doesn't work. Do you know why?
The function REGEXP returns 1 if the pattern matches and will return the full match as the pattern does match the example strings.
In your pattern you are repeating a capturing group, which will capture the last value of the iteration in group 1, but it still contains a leading forward slash that you don't want in the output.
What you might do is match the first /, and then use a quantifier {3} to repeat exactly 3 times a part ending on a / using a non capture group.
Then capture the filename in group 1, and refer to that group using '$1' in the replacement using REGEXP_REPLACE
^/(?:[^/]*/){3}(\S+\.[^.\s]+)$
Regex demo | Mysql with replace demo

Log parser: Using a text file as an input in WHERE clause

I am working on a batch script where I am parsing IIS logs using Log Parser.
So, I have 2 questions here:
Q1. What I want is, not to see the entries having username written in a text file. So, one can update that text file without worrying about the code syntax.
In other words, Instead of putting every username(could be around 30-50) in 'WHERE' clause using 'AND' again & again, I will have a text file having list of usernames.
Code Example:
"LogParser.exe" -i:csv "SELECT DISTINCT date, cs-username, clientun, cs-uri-stem FROM D:\temp.csv WHERE NOT cs-username IN ('NULL';'abc';'def';'hij';'and_so_on')" >D:\final_output.txt -o:NAT -rtp:-1
I was trying to do this via sub-query first, but it is not supported in Log parser it seems. I found OPENROWSET as a solution here but that doesn't seems to be working for me or may be I am not getting how to make it work.
Other solution I found were for SQL and were not working for log parser.
Q2. I want the logs to be read between the dates mentioned. So, I am taking a start and an end date from user in YYYY-MM-DD format and putting them in query
"LogParser.exe" -i:iisw3c "SELECT DISTINCT cs-username, REVERSEDNS(C-IP), date, cs-uri-stem, FROM \logs\location\* WHERE date BETWEEN %date_1% AND %Date_2%" >D:\temp.csv -o:csv
The error I get here is:
Error: WHERE clause: Semantic Error: left interval of BETWEEN operator ("'2016-02-15'") has a different type than operand ("date")
Please note, the output file generated from the Q2 is used as input in Q1.
Q1: Instead of filtering the data using logparser use findstr and the /g:file /v switches to filter input files or output lines (depending on the case)
Q2: Strings are not timestamps. Use
BETWEEN TO_TIMESTAMP('%date_1%','yyyy-MM-dd') AND TO_TIMESTAMP('%date_2%','yyyy-MM-dd')

Group by version number in VARCHAR field?

On my website, I have an administrator page containing download statistics. I want to know, which files are downloaded most. However, everytime I release a new version (e.g. 1.3.0 -> 1.3.1) I get a new record on the table you can see below.
Is there a way to group all versions of one item together using MySQL?
The Version is represented in different ways:
It's either 1.0, 1.00 or 1.0.0
There's .zip or .exe behind it or SomeWord.zip (e.g. "Source" or "Binaries")
The database table contains a VARCHAR field with the entire path of the download. For example
[Path] is "downloads/coding/electronics/Floppy Drive Music 3.1.zip"
Update
Comment #1 suggests storing the versionless name in the database. I hereby also accept any answers which explain how to properly remove versions from the file name using PHP.
For further understanding and disclosure, please take a look at the screenshot:
Since it's not likely being done using MySQL or in a pretty way at all, I followed the advice of comment #1 of the question:
I strip the version using PHP and group by the simplyfied string using MySQL.
$simpleFileName = substr($row['Path'], strrpos($row['Path'], '/') + 1); // Strip path, get file name only
if (preg_match('/\d+(?:\.\d+)+/', $simpleFileName, $matches))
{
// Replace version with "[VERSION]", which is processed in the front end.
$simpleFileName = str_replace($matches[0], '[VERSION]', $simpleFileName);
}

Format list of urls in mysql

I have a list of a million or urls in an mysql table.
I need to cleanse the data (extract domains) so I can be confident about DISTINCT type queries.
Data is in several different types: -
www.domain.tld
domain.tld
http://domain.tld
https://vhost.domain.tld
domain.tld/
There are invalid domains and empty data.
Ideally I'd like to do something along the lines of : -
UPDATE table1 SET domain = website REGEXP '^(https?://)?[a-zA-Z0-9\\\\.\\\\-]+(/|$|\\\\?)'
domain being a new empty field, website being the original url.
You can't use regex like that in MySQL as is, but apparently you can some some UDFs that implement it. See:
How to do a regular expression replace in MySQL?
https://launchpad.net/mysql-udf-regexp
http://www.mysqludf.org/lib_mysqludf_preg/

Extracting MySQL data within "tags" using regular expressions? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Simulating regex capture groups in mysql
Good day,
I have many rows of data stored in a MySQL table. A typical value could look something like this:
::image-gallery::
::gallery-entry::images/01.jpg::/gallery-entry::
::/image-gallery::
Is there a way - by means of a regular expression that I can a) extract the term image gallery from the first line (it could be any phrase, not just image-gallery) and then extract the center line as two separate values like this:
gallery-entry and then images/01.jpg
There could be many lines of ::gallery-entry:: values, and they could be called anything as well. A more complete example would be:
::image-gallery::
::title::MY GALLERY::/title::
::date::2011-05-20::/date::
::gallery-entry::images/01.jpg::/gallery-entry::
::/image-gallery::
In essence I want this information: The content type (image-gallery) in the above case, first line and last line. Then I need the title as a key value style pair, so title as the key and MY GALLERY as the value. Then, subsequently, I would need all the rows of fields thereafter (gallery-entry) as key value pairs too.
This is for a migration script where data from an old system will be migrated over to a new system with different syntax.
If MySQL select statements would not work, would it be easier to parse the results with a PHP script for data extraction?
Any and all help is always appreciated.
Kind regards,
Simon
Try this regex:
::image-gallery::\s+::title::(.*?)::/title::.*?::gallery-entry::(.*?)::/gallery-entry::\s+::/image-gallery::
Use single-line mode (/pattern/s) so the .*? chews up newlines.
Your key-value pairs will be:
title: $1 (matching group 1)
gallery-entry: $2 (matching group 2)
From simulating-regex-capture-groups-in-mysql there does not seem to be a way to easily capture groups with a regex in mysql. The reason is that MySQL does not natively support capture groups in a regex. If you want that functionality you can use a server side extension like lib_mysqludf_preg to add that capability to MySQL.
The easiest way is to extract the whole column with SQL and then do the text matching in another language (such as php).
In my tests kenbritton's regex didn't work, but building off of it the following regex worked on your test data:
::image-gallery::\s+::title::(.*?)::\/title::\s+(?:.*\s+)*::gallery-entry::(.*?)::\/gallery-entry::\s+::\/image-gallery::