How to convert a glob with braces to a regex - glob

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.

Related

Wrapping all keys in double curly in YML file with Sublime

I'd like to do a quick manipulation on a YML file that'll wrap all keys in double curly braces and strip the quotations , such as:
level:
group:
continue: "continue"
stop: "stop"
go: "go"
halt: "halt"
becomes
{{level:}}
{{group:}}
{{continue:}} continue
{{stop:}} stop
{{go:}} go
{{halt:}} halt
I'm sure there's a way to do this using multicursor, but so far no luck.
Thanks!
Use Regular Expression!
You want to do a replace (Find->Replace...), searching for (\w+?:), and replace with {{\1}}.
This should do what you expect (I'll add a little explanation when I'm back on my laptop).
\w is for any alphanumeric characters, and things like _, so that would be your identifier
+?: any number (but at least one) of said set of character, but in a non-greedy way (or it would also match the : I put after)
: is obviously the end of your identifier
(...) means you capture everything inside, and store it in \1
That's why the replace is: {{\1}}
Demo:
http://regex101.com/r/yE5pM1/1

Regex string with three wildcards, replace with second and third only

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.

How to search for pattern in multiple lines using a regular expression

Consider the pattern is:
PPP(GJ) {
__hj_o:
}
What is the regular expression match the above pattern?
Tcl's regular expressions can contain newlines just fine, but for anything complicated it can help to put it in its own variable instead of having it as an inline literal:
set RE {PPP(GJ) {
__hj_o:
}}
if {[regexp $RE $someString]} {
# We got a match!
}
Indeed, regexp would also match the above with this:
set RE {PPP(GJ)\s+{\s+__hj_o:\s+}}
because newlines are just ordinary whitespace characters (i.e., are matched by \s and .) by default. (The above REs are probably not exactly what you want; they likely need suitable patterns for the non-whitespace portions as well.)
However, you need to ensure that the string you are matching against has the whole thing that you want to match. If you're just feeding through one line at a time, that multiline pattern will consistently fail. This sounds obvious, but it is the easiest mistake to make.

Regex for start with three alpha and four digits

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}

Finding duplicate consecutive strings in vim using vim regex

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