Ruby regex- matching text if it doesn't start with a character - ruby

I was wondering how you are able to match a pattern only if it doesn't start with a specific character. I would like to match "foo" but I would not like to match "afoo." What kind of regex operator do I need for that? I can't seem to find the right one. Maybe an anchor?
For example I'd like to change
foo foo afoo foo
to
bar bar afoo bar
Thanks.
Although the answer below is correct for my example, what about if it was /foo instead of afoo? That doesn't seem to behave the same?

Sounds like you're looking for a negative look behind. If you say (?<!expr1)expr2 then it will match whatever expr2 matches as long as it isn't immediately preceded by something expr1 matches. For example:
>> 'foo foo afoo foo'.gsub(/(?<!a)foo/, 'bar')
=> "bar bar afoo bar"

str = "foo foo afoo foo"
str.gsub(/\bfoo/, "bar") #=> "bar bar afoo bar"

Related

Display Unique Shell Columns

Given we have two formatted strings that are unrelated to each other.
#test.rb
string_1 = "Title\nfoo bar\nbaz\nfoo bar baz boo"
string_2 = "Unrelated Title\ndog cat farm\nspace moon"
How can I use ruby or call shell commands to have each of these string display as columns in terminal? The key is that the data of each string are not building a correlated row, ie this is not a table, rather 2 lists side by side.
Title Unrelated Title
foo bar dog cat farm
baz space moon
foo bar baz boo
You can try using paste and column command together. Note that this is a shell command so spaces between the assignment operator should be corrected.
$ string_1="Title\nfoo bar\nbaz\nfoo bar baz boo"
$ string_2="Unrelated Title\ndog cat farm\nspace moon"
$ paste -d '|' <(echo -e "$string_1") <(echo -e "$string_2") | column -s'|' -t
Title Unrelated Title
foo bar dog cat farm
baz space moon
foo bar baz boo
We paste the lines with | as delimiter and tell column command to use | as a reference to form columns.
In Ruby, you could do it this way:
#!/usr/bin/env ruby
string_1 = "Title\nfoo bar\nbaz\nfoo bar baz boo"
string_2 = "Unrelated Title\ndog cat farm\nspace moon"
a1 = string_1.split("\n")
a2 = string_2.split("\n")
a1.zip(a2).each { |pair| puts "%-20s%s" % [pair.first, pair.last] }
# or
# a1.zip(a2).each { |left, right| puts "%-20s%s" % [left, right] }
This produces:
Title Unrelated Title
foo bar dog cat farm
baz space moon
foo bar baz boo
Hi , If you Use temp files
string_1 = "Title\nfoo bar\nbaz\nfoo bar baz boo"
string_2 = "Unrelated Title\ndog cat farm\nspace moon"
echo -e $string_1 >a.txt
echo -e $string_2 >b.txt
paste a.txt b.txt
I hope it will help.

Cocoadialog dropdown with array as list

Why is the foo element not included?
items=( "invisible below" foo "invisible above" "bar" "foo" not invisible )
# invisible: ^
CocoaDialog standard-dropdown --text "Choose:" --items "${items[#]}" --string-output --float --debug
The problem is that CocoaDialog loads the list of values into an array in which the keys and values are the same. It essentially sees foo and "foo" as them same item, the second one will overwrite the first - much like an array in PHP.
So, if you change your array to this:
items=( "invisible below" foo1 "invisible above" "bar" "foo" not invisible )
You'll see that foo1 shows up:
image http://img269.imageshack.us/img269/6738/screenshot20110818at223.png
The reason we know that it is CocoaDialog and not bash is that we can print out the array of items:
$ items=( "invisible below" foo "invisible above" "bar" "foo" not invisible )
$ printf "%s\n" "${items[#]}"
invisible below
foo
invisible above
bar
foo
not
invisible
So, the array that you're passing is fine - CocoaDialog is just overwriting the first value with the second one.

Bash: How do I create a variable containing a list of arguments with multi-word tokens?

I want to create a variable which is essentially a list of arguments to pass to a command or function. The pattern below with arg_string works well for foo, bar, and baz, but not for "multi word token", which I would like the command to see as a single argument.
#!/bin/bash
function func() {
for arg in "$#"
do
echo ${arg}
done
}
arg_string="foo bar baz \"multi word token\""
arg_string="foo bar baz multi\ word\ token"
arg_string="foo bar baz 'multi word token'"
func ${arg_string}
Here is the output:
foo
bar
baz
'multi
word
token'
When I want:
foo
bar
baz
multi word token
Just stick an eval before the function call:
eval func ${arg_string}

Succinct way in Ruby to manipulate this string

Sometimes I like learning how to do things the "Ruby" way. I was wondering - what is the most succinct, yet readable way to take a string such as:
foo-bar
and manipulate it to read:
Foo Bar
"foo-bar".split("-").map(&:capitalize).join(" ")
"foo-bar".gsub(/\b(\w)/){|m| m.capitalize}.sub '-', ' '
>> p "foo-bar".scan(/\w+/).map(&:capitalize).join(" ")
"Foo Bar"
=> "Foo Bar"
>> p "foo---bar".scan(/\w+/).map(&:capitalize).join(" ")
"Foo Bar"
=> "Foo Bar"
>> p "foo 123 bar".scan(/\w+/).map(&:capitalize).join(" ")
"Foo 123 Bar"
=> "Foo 123 Bar"
string = "foo-bar"
"foo-bar".split("-").map(&:capitalize).join(" ") # edited to because former answer was not optimal

Redmine plugin that replaces words via regular expression?

I'm very new to Redmine/Ruby trying to achieve a simple plugin that takes the current wiki page content and matches/replaces everytime a word occurs via regular expression. How can I do this?
Thanks!
Dennis
The word replacement can de done by using gsub() with \b to match a word boundary:
irb(main):001:0> 'foo bar baz foo bar'.gsub /\bfoo\b/, 'replaced'
=> "replaced bar baz replaced bar"
Here is a more complete solution with a dictionary of words to replace:
repl = {'foo'=>'apple', 'baz'=>'banana'}
s = 'foo bar baz foo bar'
for from, to in repl:
s = s.gsub /\b#{from}\b/, to
end
Result: apple bar banana apple bar

Resources