Creating an array that only contains the letters from a string - arrays

So I'm trying to change the string \t\n into an array of all of word characters in the string. The array I want would look like this: ["t","n"].
So far I've tried:
input = " \t\n"
array = input.scan(/\w/)
I've tried this regular expression on this string on rubular and it matches with all of the word characters as I'd like it to.
However, when using input.scan(/\w) an empty array is returned.
Please forgive my ignorance as I'm still new to this, but why is this?

Here you go! You were really close.
input = " \t\n"
array = input.dump.scan(/\w/)
=> ["t", "n"]
The key is to use String#dump (see: https://ruby-doc.org/core-2.6.5/String.html#method-i-dump)

I am not familiar with ruby but you seem to be having string interpolation confusion.
Per https://www.ruby-forum.com/t/new-line-in-string/176797
input = " \t\n"
Gives you a string with a space, tab, and newline.
You probably want to use single quotes to literally get the string you wrote:
input = ' \t\n'
If you sorely want to stick with double-quotes then I believe this would work:
input = " \\t\\n"
You should read https://blog.appsignal.com/2016/12/21/ruby-magic-escaping-in-ruby.html to learn more about string interpolation in Ruby. I would link you to the official docs but my lack of ruby experience translates to a lack of official doc experience.

So like colleagues explain in comments, the letters which you have in "\t\n" string are not ordinary letters, only something called special characters so I am not sure but there is not easy way to take this characters from this string cause \t is like one character.
With normal string like tn you could do something like this
"tn".split("")
and that give you array which you want.
But on special characters like in the example. you could do something like this
a = "\t\n".split("")
a.map! do |e|
if e == "\t"
"t"
elsif e == "\n"
"n"
end
end
which give you, I believe, results which you want.

Related

How to escape a character in bytearray

I am creating a bytearray from a list.
mybytes_array = bytes([255,110,41,128,09])
I then uses regex to find all occurences of
[(m.start(0), m.end(0)) for m in re.finditer(mybytes_array, ba)]
I can have any value instead of 41 that creates a metacharacter for regex. I want to escape that character so that I can match it against ba that is also a bytearray
How can I do that?
I cannot obviously convert to string append backslash and then match against ba. So I am not sure how can I change the mybytes_array so as to search the correct string.
The re package can work on both str and bytes inputs as long as the arguments are of the same type.
You may use re.escape to escape the whole bytes.
Your code will be something like
[(m.start(0), m.end(0)) for m in re.finditer(re.escape(mybytes_array), ba)]

Array of strings with re

This answer suggested me a code for having an array of conditions for replacements. Now I want to have one more condition which cannot be coded in the same way.
kxyz
sxyz
pxyz
clxyz
bookabcd
lookabcd
cookabcd
packabcd
bank
lab
court
catch
This is the updated word list
import re
# List where first is pattern and second is replacement string
replacements = [("ing$", "xyz"), ("ed$", "abcd")]
with open("new_abcd.txt", "w") as new, open("abcd.txt") as original:
for word in original:
new_word = word
for pattern, replacement in replacements:
new_word = re.sub(pattern, replacement, word)
if new_word != word:
break
new.write(new_word)
Let's say I want to code a conditions for words like 'bank', 'lab', 'court', 'catch' that says add "x". One may wonder what is a pattern in these words. It's nothing but all of these words are consonant ending. I don't know the Python way of doing this, but I want something like if the word does not end in ("a" or "e" or "i" or "o" or "u") change it to something else. Can re handle this?
It is not hard to check with regex whether the word ends in a non-vowel, but in this case you would not replace but just if you have a match, add a letter to the word.
The regex which will check the non-existence of a vowel at the end of the given string is
[^aeiou]{1}$
Then in python I guess do something like
regexp = re.compile(r'[^aeiou]{1}$')
if regexp.search(word):
word += 'x'
# do more stuff
I would check thoroughly whether python built in ways to do this are not faster.

Read from array1 write from array2

Just learning some basic Ruby concepts as a beginner. Not really looking for code as such, rather some fundamental principles behind the following question (obviously feel free to express yourself with code if you need to :0)
In a simple redact text exercise, a user enters some text, then enters the word to be redacted, I'm fine with this and can make it work a number of ways.
However...
to deal with the possibility the user could enter upper and/or lower case letters for either the text or redacted word, I would need to create variables .downcase! again no problem there. But what if once the program runs, you want to return the words to their original state?
I thought perhaps you would need to create an array for the original text, where each word has an index within the array, create a corresponding array with the lowercase letters and if a word is NOT redacted, then you would compare the index from the lowercase array and write the corresponding index from the original array... does this sound correct or am I over thinking it, is there an easier way?
Thanks for your help
puts " What is your message"
text1 = gets.chomp
text2 = text1.downcase
puts "What is your secret word"
redact = gets.chomp.downcase!
words = text2.split (" ")
words.each do |x|
if
x == redact
print "REDACTED" + " "
else
print x + " "
end
end
I've added my working code, you can see that I've separated text1 the original from text2 which isn't strictly necessary as it stands, but to maintain the original formatting
Your solution sounds like it could work and as a beginner it may be useful to write a complete solution like that. But don't forget that ruby can do a lot of fun stuff for you.
Lets say we take input into sentence and the string to redact is stored in redact.
We can do something as simple as this:
sentence.gsub(/#{redact}/i, "*" * redact.length)
gsub finds all occurrences of the first argument and replaces it with the second, returning a new string.
First notice that we are using the redacted string as a regular expression for the first arg and the i indicates that it should match case insensitive, as you wanted.
Now the second arg is simply a string of asterisks of equivalent length to the redacted string.
For example if we have the following:
sentence = 'this is My Sentence'
redact = 'my'
puts sentence.gsub(/#{redact}/i, "*" * redact.length)
The above method will print this is ** Sentence.
Just one extra thing to note: this regex will match all occurrences of the string. For example, if redact = 'is', the resulting sentence will be th** ** My Sentence. You can re-write the regex to avoid this if that's not the expected use case.

Using sscanf to read strings with white spaces in C

I am trying to get use sscanf to scan a string of text and store the values into an array. When it comes to storing the last string it stops scanning when it comes to a white space. For example in the below string it would only store the word "STRING". I have tried using %[^ \t\n] and the other various specifiers but it seems I am missing something.
I just cant get the function to include white space, im sure its probably something simple.
string test = "9999:STRING OF TEXT";
scan = sscanf(test, "%d:%s", rec[i].ref, rec[i].string);
You should have posted a minimal working code.
However, the issue is most likely that %s does not skip white space as do the numerical formats such as %f and %d. Use something like sscanf(test, "%d:%[^\n]", rec[i].ref, rec[i].string); to capture whatever is after :.
Look here for details: [http://www.cplusplus.com/reference/cstdio/sscanf/][1]
So this does not work?
sscanf(test, "%d:%[^\t\n]", rec[i].ref, rec[i].string);
check out this answer : reading a string with spaces with sscanf
Basically this will will match the number, followed by anything that is not in the brackets (tab, newline), note the ^ symbol.

Writing printf(#"") with multiple line string

Is it possible to write something like this:
printf(#"
-
-
-
-
");
I can do it in C#, but can't in C. It gives me an error in CodeBlocks. Am I allowed to do such ?
Error message: error: stray '#' in program.
No. That syntax doesn't exist in C.
If you want a multiple-line string, write it as multiple double-quoted strings with no other tokens in between them. They will be combined.
printf(
"some string"
"more of the string"
"even more of the string"
);
(You will, of course, need to add a \n at the end of each line if that's what you want.)
No that's not a syntax that C understands, C doesn't have raw literals.
You can use \ as the last character to continue on the next line:
const char *str = "hello\n\
world";
Also, consecutive string literals will be concatenated. So you can do e.g.
const char *str = "Hello\n"
"world\n";
C#'s verbatim strings are not available in C. If you have some characters to escape, like " or \, escape them with '\', there is no there option in this language.
If you want to embed multiple lines in a string literal, you can either insert \n at the appropriate location in your string, or escape the return character as well:
printf("Here's\
a multiline\
string litteral");
Line continuation with \ at the end of the line.
printf("\
\
-\
-\
-\
-\
");
String literals in C may not contain newlines. You have two workarounds:
Use implicit string concatenation (done by the compiler).
printf("The quick brown"
" fox jumps over"
" the sleazy dog.");
Escape the newline by placing a backslash in front of it.
printf("The quick brown\
fox jumps over\
the sleazy dog.");
Personally, I prefer the first form since the second looks ugly (my opinion) and forces you to ruin your code indentation.
In either case, the string will simply not contain the newlines. So if you really meant for them to be there, you'll have to add them via \n.

Resources