This question already has an answer here:
What is the Ruby equivalent of preg_quote()?
(1 answer)
Closed 7 years ago.
Lets say I want to match against CVS comments like:
// $Source$
My regex currently looks like this:
if ( /^\/\/\s*\$Source\$/ =~ line)
Which works, but I'm left wondering -- is there a prettier way to write this?
Use the %r syntax:
if ( %r{//\s*\$Source\$} =~ line)
^^ I'm not sure whether ruby would allow unescaped `$` here or not
Related
This question already has answers here:
What is file globbing?
(1 answer)
Stop shell wildcard character expansion?
(4 answers)
command line * linux [duplicate]
(1 answer)
Closed 4 years ago.
I stumbled upon this while working through the exercises in K&R2. Why does echo * prints the names of all files in the current directory? More generally, when I write a C program that takes command-line arguments, and when I give it * as an argument, it puts the names of all files in its parent directory in to the argument vector. Why does this happen? What is so special about *?
I could not find anything about this in the internet.
This is called globbing. Here's a detailed description. Other wildcards include ? for one character, [abc] for one of a set of characters, and [a-z] for one of a range of characters. This is built into various shells, including Bash.
In response to your comment "I think echo is written in C" — this doesn't matter a bit. Once source code is compiled into an executable containing machine code, it doesn't matter what language it was written in.
This question already has an answer here:
Bash: manipulating with strings (percent sign)
(1 answer)
Closed 6 years ago.
I've got the following variable set in bash:
ver=$(/usr/lib/virtualbox/VBoxManage -v | tail -1)
then I have the following variable which I do not quite understand:
pkg_ver="${ver%%r*}"
Could anyone elaborate on what this does, and how pkg_ver is related to the original ver value?
It is a bash parameter expansion syntax to extract text from end of string upto first occurrence of r
name="Ivory"
printf "%s\n" "${name%%r*}"
Ivo
${PARAMETER%%PATTERN}
This form is to remove the described pattern trying to match it from the end of the string. The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching.
You will get everything from variable ver until first "r" character and it will be stored inside pkg_ver.
export ver=aaarrr
echo "${ver%%r*}"
aaa
This question already has answers here:
Remove a fixed prefix/suffix from a string in Bash
(9 answers)
Closed 6 years ago.
I learned how to remove prefix and suffix respectively as below:
p="prefix-foo-bar-suffix"
echo ${p#prefix} # -foo-bar-suffix
echo ${p%suffix} # prefix-foo-bar-
and I am trying to figure out how to remove them together based on the examples above. I tried the code below but it does not work.
echo ${p#prefix%suffix} # prefix-foo-bar-suffix, looks like it treats "prefix%suffix" as a whole thing
echo ${{p#prefix}%suffix} # error, bad substitution
P.S. I know it should be easy to make it work using regex, but here I want to know if it is possible to construct a solution that just builds on top of the # and % tricks. Also, using eval may make it very easy, but as some people suggest, I tend to avoid it here.
Introduce a helper function:
$ trim() { local x="${1#"$2"}"; echo "${x%"$3"}"; }
$ trim prefix-foo-bar-suffix prefix suffix
-foo-bar-
$ trim prefix-foo-bar-suffix prefix
-foo-bar-suffix
$ trim prefix-foo-bar-suffix "" suffix
prefix-foo-bar-
Thank you for a good question.
joint use a prefix and suffix is not possible, or do I just could not find it in the documentation.
but if you know the length of a suffix and a prefix that you may fit this simple solution:
echo ${p:6: $[ ${#p}-12 ]}
This question already has answers here:
Ruby - replace the first occurrence of a substring with another string
(3 answers)
Closed 6 years ago.
I have this
2016-05-20T13:36:29.835, CTF3D57C
and I want this
2016-05-2013:36:29.835, CTF3D57C
I just want to remove the first T character. How do I do this?
This will substitute the first 'T' in string with anything you want:
str = str.sub('T', '')
If you wish to substitute all occurrence of any substring or regex, use gsub
This question already has answers here:
How do you do block comments in YAML?
(11 answers)
Closed 6 years ago.
I know that you can make a single line comment in YAML by using the # tag, but I haven't been able to find something like /* in java that starts a comment & has to be finished off with a */. Does such an operator exist in YAML?
YAML does not support multiple line comments. If you want to use them. You can just try
# this
# is a multiple
# line comment