I am trying to make an angularjs syntax highlighting file for vim. A piece of the file is:
syn match ngMethods /\.[0-9A-Za-z_\-\$]\+\s*\((\|=\)/ contains=AngularMethods
syn keyword AngularMethods contained $addControl $anchorScroll $animate ...
syn match ngObjMethods /^\s*[0-9A-Za-z_\-\$]\+\s*:/ contains=AngularObjectMethods
syn keyword AngularObjectMethods contained compile controller link ...
etc...
Down below I have:
hi def link AngularMethods Function
hi def link AngularObjectMethods Function
The first regular expression (for AngularMethods) is supposed to capture things like $addControl in the following:
myelement.$addControl()
myelement.$addControl = function ()
The second regular expression (for AngularObjectMethods) captures things like compile in:
compile : function () {}
The AngularMethods one does NOT work but the latter one does. Can anyone see the problem? I've also tried using the regexes:
/\.\zs[0-9A-Za-z_\-\$]\+\ze\s*\((\|=\)/
/\.[0-9A-Za-z_\-\$]\+\s*\((\|=\)\#=/
The former matches the exact word. The latter is something I saw in another syntax file. Any ideas? Thanks for your help!
Edit:
Kent (below) was correct about the keyword. This uncovered the real problem which is that I have another regex:
syn match ngProperties /\.[0-9A-Za-z_\-\$]\+\s*[^(=]/ contains=AngularProperties
syn keyword AngularProperties contained $attr $dirty $error ...
which is supposed to be the complement of the ngMethods regex. If I comment out the ngProperties regex, the ngMethods regex works. This means ngProperties is bad. It is supposed to catch things like $attr in:
var myAttribute = element.$attr;
I will try to fix this. Can someone post the correct regex just in case?
The regex is not the problem for your syntax.
What very likely the cause of the problem is, your iskeyword option doesn't have the dollar ($) sign.
what you can test is:
remove the $ from contained $addControl $anchorScrol, to see if it will work
or
execute: set iskeyword+=$ to see if it works.
Related
I have a Regex that looks like this:
(?<Number>\d{3})-?(?<Hand>R?L?)[-\s]?(?<Description>.*?)?(?<ShnOpp>SHN|OPP)?$
With some sample data:
104-RL-BLAH BLA SHN
104-RL FOO OPP
102-RL-BAR WL74
102-BAR WL74
102-R-BAR WL74 SHN
102-R-BAR WL74 OPP
So, the named group Hand can either contain RL|R|L|{Blank}.
But, if and only if, Hand="RL" do I want to match ShnOpp with SHN|OPP, otherwise just leave it as part of the description. So, can I do a literal IF condition within my regex?
Either my Googling skills failed me or maybe you just can't do it, but I'd love to be proved wrong.
Here's a link to a working sample: https://regex101.com/r/wGghbV/2
You can't use a conditional to check that a certain group captured one exact text, however it is possible to use a conditional here by adding a new group that only matches RL like:
(?<Number>\d{3})-?(?<Hand>(?<RL>RL)|[RL]?)[ \-]?(?<Description>.*?)[ \-]?(?(RL)(?<ShnOpp>SHN|OPP)?)$
Your updated sample: https://regex101.com/r/wGghbV/3
I have a Regex :
\*777\*[0-9]{10,}\*\d+\*(5|10|20|25|50|100)\*\d+#
That is what i have these far.
It could handle input : *777*9283928839*89*5*9090#.
The format goes like this : *777*phone*Qty*Item Code*pin#
The problem is sometime the input will go like this :
*777*phone*Qty*Item Code*Qty*Item Code*Qty*Item Code*pin#
It will repeat at Qty*Item Code. But the Item code should be one of these 5,10,20,25,50,100
I confuse in making the regex check for Qty*Item Code.
Can someone give a hint?
Thanks.
You can use the following:
\*777\*[0-9]{10,}\*(\d+\*(5|10|20|25|50|100)\*)+\d+#
Explanation
The part that's repeating seems to be this:
\d+\*(5|10|20|25|50|100)\*
If you enclose that in parentheses and add + after it, it will tell regex to match what's inside the parentheses one or more times:
(\d+\*(5|10|20|25|50|100)\*)+
I am looking for a regular expression to match all references to SuppressMessage in a solution that took over.
example:
[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString")]
I tried this to find the SuppressMessage with the beginning and ending square brackets but it does not observe line feeds and when multiple matches are with the same file, it will return the bulk of the file.
\[(SuppressMessage)\((.*)\)\]
[(SuppressMessage)((.*?))]
try it
Thanks vks - That got me closer but that finds two groups.
SupressMessage
"Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0"
What I found that works (without multiple SuppressMessages in the same square brace) is:
\[(SuppressMessage.*?)\]
\[(SuppressMessage\((?:.*?)\))\]
make your expression non greedy.In fact try
\[(SuppressMessage\((?:[^)]*)\))\]
or
\[(SuppressMessage[^)]*\)))
to make it fail proof.
I've searched around quite a bit now, but I can't get any suggestions to work in my situation. I've seen success with negative lookahead or lookaround, but I really don't understand it.
I wish to use RegExp to find URLs in blocks of text but ignore them when quoted. While not perfect yet I have the following to find URLs:
(https?\://)?(\w+\.)+\w{2,}(:[0-9])?\/?((/?\w+)+)?(\.\w+)?
I want it to match the following:
www.test.com:50/stuff
http://player.vimeo.com/video/63317960
odd.name.amazone.com/pizza
But not match:
"www.test.com:50/stuff
http://plAyerz.vimeo.com/video/63317960"
"odd.name.amazone.com/pizza"
Edit:
To clarify, I could be passing a full paragraph of text through the expression. Sample paragraph of what I'd like below:
I would like the following link to be found www.example.com. However this link should be ignored "www.example.com". It would be nice, but not required, to have "www.example.com and www.example.com" ignored as well.
A sample of a different one I have working below. language is php:
$articleEntry = "Hey guys! Check out this cool video on Vimeo: player.vimeo.com/video/63317960";
$pattern = array('/\n+/', '/(https?\:\/\/)?(player\.vimeo\.com\/video\/[0-9]+)/');
$replace = array('<br/><br/>',
'<iframe src="http://$2?color=40cc20" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');
$articleEntry = preg_replace($pattern,$replace,$articleEntry);
The result of the above will replace any new lines "\n" with a double break "" and will embed the Vimeo video by replacing the Vimeo address with an iframe and link.
I've found a solution!
(?=(([^"]+"){2})*[^"]*$)((https?:\/\/)?(\w+\.)+\w{2,}(:[0-9]+)?((\/\w+)+(\.\w+)?)?\/?)
The first part from (? to *$) what makes it work for me. I found this as an answer in java Regex - split but ignore text inside quotes? by https://stackoverflow.com/users/548225/anubhava
While I had read that question before, I had overlooked his answer because it wasn't the one that "solved" the question. I just changed the single quote to double quote and it works out for me.
add ^ and $ to your regex
^(https?\://)?(\w+\.)+\w{2,}(:[0-9])?\/?((/?\w+)+)?(\.\w+)?$
please notice you might need to escape the slashes after http (meaning https?\:\/\/)
update
if you want it to be case sensitive, you shouldn't use \w but [a-z]. the \w contains all letters and numbers, so you should be careful while using it.
I have the following statement:
Directory.GetFiles(filePath, "A*.pdf")
.Where(file => Regex.IsMatch(Path.GetFileName(file), "[Aa][i-lI-L].*"))
.Skip((pageNum - 1) * pageSize)
.Take(pageSize)
.Select(path => new FileInfo(path))
.ToArray()
My problems is that the above statement also finds the file "Adali.pdf" which it should not - but i cannot figure out why.
The above statement should only select files starting with a, and where the second letter is in the range i-l.
Because it matches Adali taking 3rd and 4th characters (al):
Adali
--
Try using ^ in your regex which allows looking for start of the string (regex cheatsheet):
Regex.IsMatch(..., "^[Aa][i-lI-L].*")
Also I doubt you need asterisk at all.
PS: As a sidenote let me notice that this question doesn't seem to be written that good. You should try debugging this code yourself and particularly you should try checking your regex against your cases without LINQ. I'm sure there is nothing to do here with LINQ (the tag you have in your question), but the issue is about regular expressions (which you didn't mention in tags at all).
You are not anchoring the string. This makes the regex match the al in Adali.pdf.
Change the regex to ^[Aa][i-lI-L].* You can do just ^[Aa][i-lI-L] if you don't need anything besides matching.
You should to do this
var f = Directory.GetFiles(tb_Path.Text, "A*.pdf").Where(file => Regex.IsMatch(Path.GetFileName(file), "[Aa][i-lI-L].pdf")).ToArray();
When you call ".*" Adali accept in Regex