ruby : regex replace string beetween two char [ ] - ruby-on-rails

i want to remove text in string beetween [ ]:
ex:
Hello everyone [hi hi], hello world [ha ha]
result:
Hello everyone, hello world
i use
string.gsub!(regex-here , ' ')
help me define a regex-here.

As I understood, you do not want leading/trailing spaces to be left in the result:
▶ str = "[ho ho] Hello everyone [hi hi], hello world [ha ha]"
▶ str.gsub /\s*\[.*?\]\s*/, ''
#⇒ "Hello everyone, hello world"

Try this:
\s*\[.*?\]
Demo
Explanation:
\[: matches ]
.*?: matches any character as few times as possible, expanding as needed

Related

How to print on same line using two print statement in lua

Example:
Input:
Print("hello")
Print("hi")
Output:
hello
hi
I want to output both "hello" and "hi" on the same line like hellohi without using concatenation.
You can achieve what you want using io.write(). This does not write a line break by default.
io.write("hello")
io.write("hi")
Output:
hellohi
To add line breaks simply add \n to your io.write e.g.
io.write("hello\nhi")
output:
hello
hi
Moreover, if you want to write a variable with a string:
Age = 18
print("Age: " .. Age)
where the .. concatenates them together.

How to build parameters for System.cmd?

I want to use System.cmd run "convert" from ImageMagick, but I am having difficulty
System.cmd("convert", ["origin.jpg", "-fill", "black", "-pointsize", "12", "-gravity", "SouthWest", "-draw", "\"text +4,+4 'Test hello world!'\"", "output.jpg"])
The args -draw 's value is \"text +4,+4 'Test hello world!'\", but ImageMagick requires "text +4,+4 'Test hello world!'" Do not need to escape double quotes.
How can I do it?
You don't need the outer double quotes here with System.cmd/3. You need them when running the command from the shell because the argument contains spaces and without the outer double quotes the shell will split the whole thing on every space and end up passing the equivalent of ["text", "+4,+4", "Test hello world!"]. The following should work:
System.cmd(..., [..., "text +4,+4 'Test hello world!'", ...])

Dispaly special characters ("#") in uitextview content

I have to display "#" in the UITextview content and after to put some information.
I looked on the internet via google but I didn't find an explanation which make
me understand the good approach.
Can you help me with some extra advice ?
Thanks !
You can simply do it like this:
_textView.text=#"#Hi Hello"; result will be #Hi Hello
However if you want to use " in the text you need to append it to backslash \
_textView.text=#"#Hi \"Hello"; result will be #Hi "Hello
You can enter almost all special characters without any problem, but you need to take care for the double quotes:
_textView.text=#"#Hi \"Hello * ! # # $ % ^ & ( ) _ + - [ ] ; ' {} <> ,. / ? : \" ";

How do I use collect keep in parse, to get embedded blocks?

Looking at the html example here: http://www.red-lang.org/2013/11/041-introducing-parse.html
I would like to parse the following:
"val1-12*more text-something"
Where:
"-" marks values which should be in the same block, and
"*" should start a new block.
So, I want this:
[ ["val1" "12"] ["more text" "something"] ]
and at the moment I get this:
red>> data: "val1-12*more text-something"
== "val1-12*more text-something"
red>> c: charset reduce ['not #"-" #"*"]
== make bitset! [not #{000000000024}]
red>> parse data [collect [any [keep any c [#"-" | #"*" | end ]]]]
== ["val1" "12" "more text" "something"]
(I actually tried some other permutations, which didn't get me any farther.)
So, what's missing?
You can make it work by nesting COLLECT. For e.g.
keep-pair: [
keep some c
#"-"
keep some c
]
parse data [
collect [
some [
collect [keep-pair]
#"*"
collect [keep-pair]
]
]
]
Using your example input this outputs the result you wanted:
[["val1" "12"] ["more text" "something"]]
However I got funny feeling you maybe wanted the parse rule to be more flexible than the example input provided?

Find the number of words in a string starting with "a"

In Ruby, I want display the count of words starting with "a" from this string: an ant hello hint how are you
You can use regex with a scan
string.scan(/\ba/).size
"a asa a ss sa a a a ass a ".split.select {|w| w[0] == "a"}.size
p "an ant hello hint how are you".split.count{|word| word.start_with?('a')}
#=> 3

Resources