i need to make a program where the input file is a source code with comments and output is the same code without comments.
It would be perfect if it would eliminate the /* */ comments also.
to solve your problem you'll have to user preg_replace
$notCommentedFile = preg_replace('/#.*/', '', $commentedFile);
$notCommentedFile = preg_replace('#//.*#', '', $notCommentedFile);
$notCommentedFile = preg_replace('#/\*(?:[^*]*(?:\*(?!/))*)*\*/#', '', $notCommentedFile);
this 3 pattern will remove the comment on your file ^^
UPDATE
ups! didn't see the comment X/ this works for php, but i think a java function like preg_replace exist and regular expression are universal :D
here an example on how to use the equivalent preg_replace in java
What is the Java equivalent to this preg_replace?
Related
Apologies in advance for a rather simplistic question.
In a big pile of "note to self", I have this little awk one-liner that works perfectly for my needs, which is to remove comments from a php file. But I would like to better understand it:
awk '/^\/\*/{c++} c!=1; /^ \*\//{c++}'
I cannot, unfortunately, find the original post in order to better understand how it works.
Googling it turns up a ton of things on c++ but not the original post. Neither Explain Shell nor Symbol Hound has helped.
I don't particularly stumped by the {c++} c!=1; bit. Any help would be greatly appreciated.
I have tested your awk script and it does not work properly!!!
You should use the following one to remove :
gawk '/^\/\*/{c++} c!=1; /^.*\*\//{c=0}' php_file
to remove /* comments */ multi-lines comments or single lines with only /* comments */!!!
TESTED on:
In a nutshell,
awk will read the input file line by line and will do its default action: printing
when the condition /^\/\*/ is met ( /* comment that is directly located at the start of the line) the block {c++} will be executed!, in awk variable are by default initiated to 0, so after the block c=1 and c!=1 will be false so nothing will be printed.
as long as we are in the comment section c!=1 will be false so nothing will be printed.
then we reach the last line of comment and c is set back to 0, so from the following line everything will be printed again.
REMARK:
lines with commands followed /* */ are still printed as-is
// comments are not removed
If there are some super important commands on the same line directly following a comment block they will be lost!!!
/* comment starts
/* still some comments
end of comments */some super important commands;
IMPROVEMENT:
change c=!1 by c<1 to improve the stability of the script, with c!=1 your script will stop functioning properly in the case:
/* comment starts
/* still some comments
end of comments */
I am having some trouble with ruby syntax in chef (the configuration management tool).
The difference I am dealing with is assigning attributes such as below:
runner = ChefSpec::Runner.new(platform: "ubuntu", version: version)
runner = ChefSpec::Runner.new(:platform => "ubuntu", :version => version)
I need to be able to switch between these two syntax's as they seem to work in different versions of chef/ruby - we will be upgrading but for now need a fix.
I'm novice with regexp, but have been trying like this in python:
for line in fileinput.input(inplace=1, backup='.bak'):
line = re.sub('(\w\w...): ',':"\1" =>', line.rstrip())
print(line)
I don't know what the attribute name will be, or what precedes it, only that a single : follows a word (of 2 or more characterss). I'm happy assuming that there will always be a space after the : but I seem to be having trouble "picking up" the attribute name to replace and re-use later.
the following should work:
for line in fileinput.input(inplace=1, backup='.bak'):
line = re.sub('(\w+): ', ':\g<1> => ', line.rstrip())
print(line)
see regex demo, or repl demo
What character can I use to put comments in an Exuberant Ctags .ctags file?
I would like to add comments with explanations, and perhaps to disable some regexps.
But I can't find any comment character which ctags-exuberant accepts!
I keep getting the warning:
ctags: Warning: Ignoring non-option in /home/joey/.ctags
which is better than an error, but still a little annoying.
I have tried # // /* ... */ and ; as comments, but ctags tries to parse them all!
Here is an example file with some comments which ctags will complain about:
# Add some more rules for Javascript
--langmap=javascript:+.jpp
--regex-javascript=/^[ \t]*var ([a-zA-Z_$][0-9a-zA-Z_$]*).*$/\1/v,variable/
--regex-javascript=/^[ \t]*this\.([a-zA-Z_$][0-9a-zA-Z_$]*)[ \t]*=.*$/\1/e,export/
--regex-javascript=/^[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*):.*$/\1/p,property/
--regex-javascript=/^\<function\>[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*)/\1/f,function/
# Define tags for the Coffeescript language
--langdef=coffee
--langmap=coffee:.coffee
--regex-coffee=/^class #?([a-zA-Z_$][0-9a-zA-Z_$]*)( extends [a-zA-Z_$][0-9a-zA-Z_$]*)?$/\1/c,class/
--regex-coffee=/^[ \t]*(#|this\.)([a-zA-Z_$][0-9a-zA-Z_$]*).*$/\2/e,export/
--regex-coffee=/^[ \t]*#?([a-zA-Z_$][0-9a-zA-Z_$]*):.*[-=]>.*$/\1/f,function/
--regex-coffee=/^[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*)[ \t]+=.*[-=]>.*$/\1/f,function/
--regex-coffee=/^[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*)[ \t]+=[^->\n]*$/\1/v,variable/
--regex-coffee=/^[ \t]*#?([a-zA-Z_$][0-9a-zA-Z_$]*):.*$/\1/p,property/
You can't! I looked through the source code (thanks to apt-get source). There are no checks for lines to ignore. The relevant code is in parseFileOptions() in options.c
But sometimes comments are a neccessity, so as a workaround I put a comment in as a regexp, in such as way that it is unlikely to ever match anything.
--regex-coffee=/^(COMMENT: Disable next line when using prop tag)/\1/X,XXX/
The ^ helps the match to fail quickly, whilst the ( ) wrapper is purely for visual effect.
Your comment should be a valid regexp, to avoid warnings on stderr. (That means unescaped /s must be avoided, and if you use any [ ] ( or )s they should be paired up.) See Tom's solution to avoid these restrictions.
As #joeytwiddle points out, comments are not supported by the parser, but there is a work-around.
Example .ctags file:
--regex-C=/$x/x/x/e/ The ctags parser currently doesn't support comments
--regex-C=/$x/x/x/e/ This is a work-around which works with '/' characters
--regex-C=/$x/x/x/e/ http://stackoverflow.com/questions/10973224/how-to-add-comments-to-an-exuberant-ctags-config-file
--regex-C=/$x/x/x/e/
--regex-C=/$x/x/x/e/ You can add whatever comment text you want here.
You can use '#' as the start of comment if you are using Universal-ctag(https://ctags.io).
Given that comments don't work, what about a .ctags.readme file...
For most things you don't actually need a comment, e.g. you don't really need the comment below.
# Define tags for the Coffeescript language
--langdef=coffee
--langmap=coffee:.coffee
I can see however that you might want to add comments explaining some mind bending regex, so for each line that absolutely needs it you can copy paste it into the .ctags.readme file as a markdown file:
Forgive me father for I have regexed
It was purely because I wanted some lovely coffee properties
```
--regex-coffee=/^[ \t]*#?([a-zA-Z_$][0-9a-zA-Z_$]*):.*$/\1/p,property/
```
Keeping .ctags.readme and .ctags in sync
You could have a block at the bottom of the ctags file separated with a line break, then delete this final block.
If you only have the one line break in your .ctags file this sed will delete all the lines after the line break.
Then do some grepping for the --regex lines to append the lines from .ctags.readme into .ctags.
sed -i '/^\s*$/,$d' .ctags
grep "^--regex" .ctags.readme >> .ctags
Is it possible to do a string substitution/transformation in Puppet using a regular expression?
If $hostname is "web1", I want $hostname_without_number to be "web". The following isn't valid Puppet syntax, but I think I need something like this:
$hostname_without_number = $hostname.gsub(/\d+$/, '')
Yes, it is possible.
Check the puppet function reference: http://docs.puppetlabs.com/references/2.7.3/function.html
There's a regular expression substitution function built in. It probably calls the same underlying gsub function.
$hostname_without_number = regsubst($hostname, '\d+$', '')
Or if you prefer to actually call out to Ruby, you can use an inline ERB template:
$hostname_without_number = inline_template('<%= hostname.gsub(/\d+$/, "") %>')
In this page:
https://blog.kumina.nl/2010/03/puppet-tipstricks-testing-your-regsubst-replacings-2/comment-page-1/
it is quite well explained and there is a fantastic trick for testing your regular expressions with irb.
Whith this link and the answer of freiheit I could resolve my problem with substitution of '\' for '/'.
$programfiles_sinbackslash = regsubst($env_programfiles,'\','/','G')
Inn ~/script.vim, I have:
set runtimepath+=string(substitute(expand("%:p"), 'script\.vim', '', 'g'))
I have an alias in .bashrc:
alias vimscript="vim -S ~/script.vim"
Running string(substitute(expand("%:p"), 'script\.vim', '', 'g')) works as intended.
The problem is when using it in the set runtimepath expression, it doesn't work when I call vimscript in terminal which calls script.vim. When I run set rtp in vim after being called by vimscript to check the runtimepath, the desired appended string isn't showed (but the other ones are there).
I have some additions to #Laurence Gonsalves answer:
There is also «concat and assign» operator: .=, so
let foo=foo.bar
can be rewritten as
let foo.=bar
Code
let &runtimepath.=','.string(path)
will append ,'/some/path' to &runtimepath, while you probably need ,/some/path.
I guess that you want to append path to your script to runtimepath. If it is true, then your code should be written as
let &runtimepath.=','.escape(expand('<sfile>:p:h'), '\,')
inside a script, or
let &runtimepath.=','.escape(expand('%:p:h'), '\,')
from current editing session (assuming that you are editing your script in the current buffer).
The right hand site of a set command is not an expression, it's a literal string.
You can manipulate options (the things set sets) by using let and prefixing the option name with an &. eg:
let &runtimepath=substitute(expand("%:p"), 'script\.vim', '', 'g')
To append to runtimepath with a let you can do something like:
let &runtimepath=&runtimepath . ',' . substitute(expand("%:p"), 'script\.vim', '', 'g')
(The . is the string concatenation operator.)
by 2022 i used this:
if ( isdirectory($some_shell_variable) )
set runtimepath+=$vi
endif
You can use a plain vim variable or whatever else just as well.