How to highlight math operators like '*' '+' '-' etc. for c language? - c

I'm writing C code and have some nice highlighting scheme but there is one thing I'd like to highlight and I can't figure out how. It's maths symbols like *;+;-;/;= ... and brackets {} [] (). I want them in the same color. I searched everywhere and the only thing I found was how to highlight specific keywords (I already used it for "FILE" keyword, I don't know why they didn't highlight it by default)

Looking at https://wiki.gnome.org/Apps/Gedit/NewLanguage which seem to be the gedit doc for how to set up cunstom sets of syntax highlights. This points to
https://developer.gnome.org/gtksourceview/stable/lang-tutorial.html
which says "Those regular expressions are PCRE regular expressions in the form /regex/options" following the documentation you can match individual characters like
<context id="escape" style-ref="escaped-character">
<match>\\.</match>
</context>
which matches \.. The doc also has examples of complete regular expressions
<match>http:\/\/[^\s]*</match>
for matching a URL.
Putting this together you might want something like
<context id="maths" style-ref="my-math-characters">
<match>(\+|\-|\*|\/)</match>
</context>
(...) is grouping, | is alternation, \+ etc quotes a meta-character. I don't use gedit myself so this is untested. It probably best to start with a simple set of symbols to see if things work at all and then expand for your complete set of characters. Watch out for < > & which have special meaning in xml so you may need to use < etc.

Related

How can I match words A or B or nothing on regex.h, C?

Development's environment is regex.h on C. It's POSIX regex.
And I putted a REG_EXTENDED option. (http://man7.org/linux/man-pages/man3/regcomp.3.html)
Here is PostgreSQL's some syntax. [] part can be ignore or use word. () part have to use one word. '|' means OR.
SET [SESSION|LOCAL] SEARCH_PATH (TO|=) (SCHEMA|'SCHEMA'|"SCHEMA");
For example, this syntax can be like this.
#1: SET SEARCH_PATH TO SCHEMA;
#2: SET SESSION SEARCH_PATH = "SCHEMA";
I've wanted to parse schema's name. But I can't make correct regex.
I made this regex When I ignored omitted part. don't considerate about case. I already made ignore case option.
^\\s*set\\s+search_path\\s+(to|=)\\s+['\"]?([a-z0-9_$$]?+)['\"]?
But it was not working if I putted session or local. So I fixed this regex little bit. But I couldn't make sure to match corrected sentences. This is what I tried.
^\\s*set\\s+(session\\s+|local\\s+)?search_path\\s+(to|=)\\s+['\"]?([a-z0-9_$$]?+)['\"]?
So I hope to make regex what can be matched correct syntax.
Edit:
I tested this and working well what I wanted. You can see what I tried on this web page. https://regex101.com/r/5zclS7/3
But on C, it's not working.

regex with OR condition not working in angularjs [duplicate]

I'm creating a javascript regex to match queries in a search engine string. I am having a problem with alternation. I have the following regex:
.*baidu.com.*[/?].*wd{1}=
I want to be able to match strings that have the string 'word' or 'qw' in addition to 'wd', but everything I try is unsuccessful. I thought I would be able to do something like the following:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
but it does not seem to work.
replace [wd|word|qw] with (wd|word|qw) or (?:wd|word|qw).
[] denotes character sets, () denotes logical groupings.
Your expression:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
does need a few changes, including [wd|word|qw] to (wd|word|qw) and getting rid of the redundant {1}, like so:
.*baidu.com.*[/?].*(wd|word|qw)=
But you also need to understand that the first part of your expression (.*baidu.com.*[/?].*) will match baidu.com hello what spelling/handle????????? or hbaidu-com/ or even something like lkas----jhdf lkja$##!3hdsfbaidugcomlaksjhdf.[($?lakshf, because the dot (.) matches any character except newlines... to match a literal dot, you have to escape it with a backslash (like \.)
There are several approaches you could take to match things in a URL, but we could help you more if you tell us what you are trying to do or accomplish - perhaps regex is not the best solution or (EDIT) only part of the best solution?

Regular expression to replace (if|then)

I have some verse references in articles that I want to link to the adjacent verses file.
Example:
some text (Gen 2:15, 16), other text (Ex 4:12, 13) more.. etc.
I could replace the first one with the following regex:
\(Gen \1: \2, \3\)
Here I fixed the "1" (book=) and the "Gen"
But I couldn't figure out how to use if|then so that I could give it all arrays of (Gen|Ex|Lev.. etc.), so that it replaces Gen with book number "1", Ex "2".. etc.
You need to somewhere define what all the book orders are. And you'll need to use some sort of scripting language, not just a plain old regex. For example, you could do something along the lines of:
books = ["Gen", "Ex", ..., "Rev"]
...and then replace book_name with books.index(book_name)+1
The exact code/syntax obviously depends on which language you choose to use.
With notepad++ you won't be able to get the order numbers.
But everything else is possible. You need to put each book on a new line:
find \), and replace by \n
Then use this pattern:
[a-z\s]+\(([a-z]+)\s+([0-9:]+)\,\s+([0-9]+)\)
and replace by:
\1: \2, \3
you'll get the list of urls. Which then you can merge back to one line if needed.
The only problem is the book number.
Demo is here: https://regex101.com/r/qN8mO7/2

Add specified comment pattern in c

I'm wondoring if there exists a function of a software tool which allows me to add empty comment pattern to the variables defined in methods in c, for example /**...*/
I've tried using eclipse and vim. The best I can do is to add just comments for functions at the begining. I'd like to know if I could add such pattern wherever I want.
I know that use short cut key like Shift+Ctrl+/ can make a sentence as comment, but in the format of //. If there's a way for me to change this format to the one I want, that would be also a great help. Thanks!
In Notepad++ you can do that!
Check this link
In the web page search for Comment / uncomment section.
With The NERD Commenter, you can surround a selected text or variable with command delimiters via its <Leader>cc mapping:
[count]<Leader>cc NERDComComment
Comment out the current line or text selected in visual mode.
With
let NERDComInsertMap='<c-c>'
you can define an insert mode mapping that inserts the comment prefix and suffix at the current position, and puts the cursor in between. The comment syntax is filetype-specific and can be configured via the 'commentstring' option.
To change the comment prefix / suffix, you have to customize the plugin (in your ~/.vimrc), as described by :help NERDCustomDelimiters, e.g. for Java:
let g:NERDCustomDelimiters = {
\ 'java': { 'left': '/**', 'right': '*/' }
\ }
For unknown filetypes, you can also use 'commentstring', as this is what the plugin falls back to.

VIM syntax: conditional function coloring

I'm customizing the standard "c.vim" syntax file in order to tune the visualisation of my C code.
I would like to distinguish the color of the "called functions" from the one of the "declared functions".
Exemple:
int declared_function()
{
int m;
m = called_function();
return (m)
}
I read in depth the VIM documentation, and millions of forums and google results, but all the solutions I tried didn't work.
To resume, I did this:
I defined a region in a recursive way in order to consider all the code within the braces:
syn region Body start="{" end="}" contains=Body
Then I defined through VIM patterns a general function syntax:
syn match cFunction "\<\h\w*\>\(\s\|\n\)*("me=e-1 contains=cType,cDelimiter,cDefine
I did this because I thought I could combine the two in a "if else" condition in the .vimrc file... but after a whole day of failing tests I need the help of someone, who can tell me if it's possible and how to do it.
Thanks everybody.
You're very close. First, you don't need the recursive definition, but contain all other top-level C syntax elements in it, plus the special group you'll define for called functions:
:syn region Body start="{" end="}" contains=TOP,cFunctionUse
Actually, scratch that, the default $VIMRUNTIME/syntax/c.vim already defines a cBlock syntax group.
Then, define a different syntax group that is contained in the cBlock group.
:syn match cFunctionUse "\<\h\w*\>\(\s\|\n\)*("me=e-1 contained containedin=cBlock contains=cType,cDelimiter,cDefine
Finally, link or define a different highlight group for it, so that it actually looks different:
:hi link cFunctionUse Special
You can put those into ~/.vim/after/syntax/c.vim, so that they'll be added automatically to the default C syntax.

Resources