I have multiple <li> in my code, well over 3,000 of them (don't ask!).
They are all either in the format:
<li>Name, Job, Company</li>
or
<li>Job, Company</li>
I need to find the ones that contain a Name (i.e. the ones with two commas ,, as opposed to just one), and remove the names. I was hoping to use Sublime Text's Regex find+replace feature.
Now, I can select all the lines that contain two commas using the following regex:
<li>.*,.*,.*</li>
But how do I now replace those with just the second and third .*s, discarding the first?
find this :
<li>.*,(.*),(.*)</li>
replace with :
<li>\1,\2</li>
or
<li>$1,$2</li>
whatever your editor supports
sed -r 's/[^,]*,([^,]*,[^,]*)/\1/g'
not .* because it would match the comma.
Related
I have this string that should be removed from the content of my wordpress website. I want it to be removed from database too.Either via Phpmyadmin or through a plugin.
Plugins don't accept wildcards or regex.
The string starts with <li class="dZip"> and ends with Download ZIP</a></li> , and contains alphanumeric and special characters between them. I like to remove all of them.
I have tried this <li class="dZip">\b.*Download ZIP</a></li>\b using plugins.No use.
If you have a new enough MySQL or MariaDB, you can use the function REGEXP_REPLACE().
The regexp would be
<li class="dZip">.*?Download ZIP</a></li>
two changes from what you had...
\b is a "word boundary". By definition either side of > is a word boundary.
So, I removed them.
.* would gobble up all the way to the last </li>. If you are expecting multiple li's then use .*? so that it gobbles only the one. The function (either MySQL's REGEXP_REPLACE or PHP's preg_replace) will repeat until finished.
I'm trying to recognize quoting (citing) somebody's else sentence in a markdown text, which I have in my local copy of MySQL GHTorrent dataset. So I wrote this query:
select * from github_discussions where body rlike '(.)*(\s){1,}(>)(\s){1,}(.)+';
it matches some unwanted data, which according to https://regex101.com/, it should not with this particular regular expression.
Test string:
`Params` is plural -> contain<s>s</s>
Matched on MySQL database, not matched at regex101 dot com.
Obvious example of quoting, but not matched at db:
Yes, I believe so.\r\n\r\n\r\n\r\nK\r\n\r\n> On 19-Jul-2014, at 17:33, Stefan Karpinski <notifications#github.com> wrote:\r\n> \r\n> This is the standard 3-clause BSD license, right?\r\n> \r\n> —\r\n> Reply to this email directly or view it on GitHub.
Moreover, MySQL workbench didn't show those return carriage and new line symbols unless copy-pasted here.
Can I normalize (remove \r and \n) with some update query ?
Is MySQL regex implementation different from POSIX standard regex ?
Do you have by any chances maximally clean solution for recognizing quoting in a markdown text ?
Thanks!
You've got an awful lot of parens in there. Try this as functionally what you have above:
select * from github_discussions where body rlike '.*[:blank:]+>[:blank:]+.+'
However, I'm not sure that's really what you want. This would happily match this line:
this is before > and after
which by my understanding is not a quoted string in markdown. Instead I would anchor it at the beginning like this:
select * from github_discussions where body rlike '^[:blank:]*>[:blank:]+'
That will match a greater-than sign at the beginning of the line, optionally preceded by whitespace. Is that what you are looking for?
I'm not sure if your data has newlines embedded. If so, you may need to look into ways of having your regex identify newlines using the ^ anchoring symbol. As is the well accepted conclusion in regex literature, that is left as an exercise for the student. :-)
I want to change a glob such as c{at,lif} in to a regex. What would it look like? I've tried using /c[at,lif]/ but that did not work.
For Basic GREP operation see f.e. http://www.regular-expressions.info/refquick.html
From http://www.regular-expressions.info/alternation.html:
If you want to search for the literal text cat or dog, separate both options with a vertical bar or pipe symbol: cat|dog. If you want more options, simply expand the list: cat|dog|mouse|fish.
This suggests the following should work:
/c(at|lif)/
Obligatory What Was Wrong With Yours, Then:
/c[at,lif]/
The square brackets [..] are not used in GREP for grouping, but to define a character class. That is, here you create a custom class which allows one of the characters at,lif. Thus it matches ca or c, or cf -- but always only one character. Adding a repetition code c[at,lif]+ only appears to work because it will then match both cat and clif, but also cilt, calf, and c,a,t.
I have writen an sql statement to retrieve data from Mysql db and I wanted to select data where myId start with three alpha and 4 digits example : ABC1234K1D2
myId REGEXP '^[A-Z]{3}/d{4}'
but it gives me empty result(data is available in DB). Could someone point me to correct way.
In most regex variants the answer would be: /d matches a / followed by a d; I think you want \d which matches a digit.
However MySQL has a somewhat limited regex implementation (see documentation).
There is no shortcut to character sets like \d for any digit.
You need to either use a named character set ([[:digit:]]), or just use [0-9].
Try this out :
[A-Z]{3}[0-9]{4}
If you want characters to be case insensitive. Try this :
[a-zA-Z]{3}[0-9]{4}
First, in regular regular expressions, to match a digit, you have to use \d instead of /d (which makes you match / followed by d).
Then, I had never noticed, but I think \d (and the others like \w, etc.) don't seem to be available in MySQL. The doc lists the accepted spacial chars, and those generic classes don't appear. You could use [:digit:] instead, even if [0-9] is quite shorter ;)
You are doing fine, just replace /d with \d.Final regex: ^[A-Z]{3}\d{4}
You could use the following pattern :
^[a-zA-Z]{3}\d{4}
So let's say I have this in my search file
Foo
Bez, Bez
Foobar
Foo
I want to search for Bez, Bez by using a regex.
This is what I have and I know it's not even remotely correct.
:%s/\([a-zA-Z]\),\([a-zA-Z])/\1,\1,\1/g
So basically what I want to do is make "Bez, Bez" into "Bez, Bez, Bez"
Really, I'm stumped on how to find 2 consecutive equivalent strings.
what about:
%s/\(\w\+\), \1/\1, \1, \1/g
it captures the expression between the parenthesis even before ending the expression whole match, pretty neat huh?.
You use capturing groups such as:
(\w+)\W+\1
but I don't recall the vim equivalent for such regex expression.
I tested using RegexPal and the input you gave
Edit
Found Back References in Vim