Logical operator placement in Ruby Regexp [duplicate] - regex

This question already has an answer here:
Regex java. Why using intersection?
(1 answer)
Closed 4 years ago.
I have this bit of Ruby code, which pulls all the vowels out of an array of strings while preserving punctuation:
def remove_vowels(arr)
arr.map do |word|
word.scan(/[\W||\w&&[^aeiou]]/).join
end
end
arr = %w(Hello! How are you? My name is Bob; what is yours?)
p remove_vowels(arr)
# => ["Hll!", "Hw", "r", "y?", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]
I'm curious why putting ||\W on the tail end of the expression, so:
word.scan(/[\w&&[^aeiou]||\W]/).join
has no effect, requiring to lead with \W|| instead. Is there some rule about the order in Regexp that explains this, or is it a simple syntax error?

|| is not OR in RegEx, and inside the [], there's no need OR.
You can simply write your regex like this: /[[\w&&[^aeiou]]\W]/. (update: or just /[^aeiou]/)
On the other hand, && is Class Intersection.
Examples:
arr
#=> ["Hello!", "How", "are", "you?", "[]||&\\", "My", "name", "is", "Bob;", "what", "is", "yours?"]
arr.map do |word| word.scan(/[[\w&&[^aeiou]]\W]/).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]
arr.map do |word| word.scan(/[[\w&&[^aeiou]]|]/).join; end # | inside [] will be read literally.
#=> ["Hll", "Hw", "r", "y", "||", "My", "nm", "s", "Bb", "wht", "s", "yrs"]
arr.map do |word| word.scan(/[[\w&&[^aeiou]]||]/).join; end
#=> ["Hll", "Hw", "r", "y", "||", "My", "nm", "s", "Bb", "wht", "s", "yrs"]
## Note this one, it is OR now:
arr.map do |word| word.scan(/[\w&&[^aeiou]]|\W/).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]
And as Mr. Swoveland correctly pointed out in comments, /[\W||\w&&[^aeiou]]/ is essentially the same as /[^aeiou]/, because the latter class actually includes \W.
Also you might want to add i flag to be case insensitive:
arr = %w(Hello! How are you? []||&\\ hELLO My name is Bob; what is yours?)
arr.map do |word| word.scan(/[\W||\w&&[^aeiou]]/).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "hELLO", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]
arr.map do |word| word.scan(/[^aeiou]/).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "hELLO", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]
arr.map do |word| word.scan(/[^aeiou]/i).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "hLL", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]

Related

Merging list based on values

I have 4 lists of different lengths, simplifying:
main_lst1= ["a", "b", "c", "d", "e", "f", "g"] - with all values
lst1 = ["a", "c", "d", "e", "f"]
lst2 = ["a", "b", "c", "d", "e", "g"]
lst3 = ["c", "d", "e", "f", "g"]
and I need to combine/ merge that lists to get:
lst1 = ["a", 0, "c", "d", "e", "f", 0]
lst2 = ["a", "b", "c", "d", "e", 0, "g"]
lst3 = [0, 0, "c", "d", "e", "f", "g"]
I hope that is enough explanation what I need to get.
Kindly help.
I'm going to assume that lst1, lst2, and lst3 are alphabetically sorted and that they would only have the characters from main_lst1.
Something like this should work:
main_lst1 = ["a", "b", "c", "d", "e", "f", "g"]
lst1 = ["a", "c", "d", "e", "f"]
lst2 = ["a", "b", "c", "d", "e", "g"]
lst3 = ["c", "d", "e", "f", "g"]
for cur_lst in (lst1, lst2, lst3):
cur_i = 0
while len(cur_lst) != len(main_lst1):
if cur_lst[cur_i] != main_lst1[cur_i]:
cur_lst.insert(cur_i, 0)
cur_i += 1
if cur_i == len(cur_lst):
cur_lst.append(0)
break
print(cur_lst)
Basically, I loop over the list and insert 0 when I find that the character doesn't match that of the main list. Of course, if the assumptions I made earlier aren't correct, then this solution will need more alterations to work.
Edit: Also note that since lists are mutable in python, the above code would alter the original lists.
This is written in Python3. Hope this helps!
main_lst1 = ["a", "b", "c", "d", "e", "f", "g"]
lst1 = ["a", "c", "d", "e", "f"]
lst2 = ["a", "b", "c", "d", "e", "g"]
lst3 = ["c", "d", "e", "f", "g"]
def reformat_list(source, target):
tmp = source
source = []
for letter in target:
if letter in tmp:
source += [letter]
else:
source += ['0']
return source
lst1 = reformat_list(lst1, main_lst1)
lst2 = reformat_list(lst2, main_lst1)
lst3 = reformat_list(lst3, main_lst1)
print(lst1)
print(lst2)
print(lst3)

Why does ugettext_lazy in rest_auth return a strange value?

I tried to response using ugettext_lazy with this code.
return Response({"detail": _("New password has been saved.")})
And the response value I expected is {"detail": "New password has been saved."
But the actual return value is
{
"detail": [
"N",
"e",
"w",
" ",
"p",
"a",
"s",
"s",
"w",
"o",
"r",
"d",
" ",
"h",
"a",
"s",
" ",
"b",
"e",
"e",
"n",
" ",
"s",
"a",
"v",
"e",
"d",
"."
]
}
Why are all characters returned separated value and wrapped in a list?
Try this;
from django.utils.encoding import force_text
return Response({"detail": force_text(_("New password has been saved."))})

Temp variable not assigning right value from array

I am writing a program on the arduino IDE where i want to swap some variables around. If you are unaware of arduino, it is essentially written in C++.
A bit of background information:
Pieces is a 2D array
Pieces will return a string of "n", "1", "2", ..., "5", or "6"
Serial.println(msg) is just a print function
Here is the code:
String temp = Pieces[piece][0];
Serial.println(temp);
Serial.println(Pieces[piece][0]);
The code should return 2 of the same values, but what it actually returns is:
""
"n"
The first value should be n as well, but it is just blank. Here is how Pieces was initialised:
String Pieces[27][6] =
{
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
{"t", "t", "t", "t", "t", "t"}
};
The problem is that the array is too large, so i've tried to change it to chars instead of strings, but its not working.
Heres what it looks like now:
char Pieces[27][6] =
{
{"n", "n", "n", "n", "n", "n"},
{"n", "n", "n", "n", "n", "n"},
...
{"t", "t", "t", "t", "t", "t"}
};
And heres the error its giving me:
error: too many initializers for 'char [6]'
Could someone help me to initialize it properly?
Since it seems to be a memory-related problem, you should consider using simple chars instead of Strings, or char[] if you really need multi-character strings.
Your initialization would then look like this:
char Pieces[][6] =
{
{'n', 'n', 'n', 'n', 'n', 'n'},
{'n', 'n', 'n', 'n', 'n', 'n'},
...
{'t', 't', 't', 't', 't', 't'}
};
and your assignment would be
char temp = Pieces[piece][0];
In case you need to use strings, use char*:
char* Pieces[][6] =
{
{ "n", "n", "n", "n", "n", "n" },
...
{ "n", "n", "n", "n", "n", "n" }
};
char *temp = Pieces[0][1];
If you are not going to perform any fancy string operations, the String class is not useful for you anyhow.
By the way, note how conveniently you can leave out the array size when initializing.

Issue with variable values in selenium grid execution

We are using selenium grid to all UI testcases.
We are facing issue when any variable having value 1 which is getting entered into any text field.
e.g.
set test variable ${data} 1
input text ${data}
if a variable has value set to 1 but below value is getting set:
/tmp/21fe989d-6c5d-413b-8365-cf6da42e92b9/upload957182740231871694file/1
Due to this testcases are failing as text field expectes int but it gets above value.
I am not sure from where "/tmp/21fe989d-6c5d-413b-8365-cf6da42e92b9/upload957182740231871694file/" this part is getting appended to the variable value.
This is strage as it happens only when value is 1.
Below is the detail log from test exection:
16:56:00.882 INFO Typing text '1' into text field 'xpath=//input[#name='floorValue']'
16:56:00.883 DEBUG POST http://192.168.1.201:4444/wd/hub/session/e8d83ee3-7a75-40cd-a079-bb2c83d6fb2e/elements {"using": "xpath", "sessionId": "e8d83ee3-7a75-40cd-a079-bb2c83d6fb2e", "value": "//input[#name='floorValue']"}
16:56:00.905 DEBUG Finished Request
16:56:00.905 DEBUG POST http://192.168.1.201:4444/wd/hub/session/e8d83ee3-7a75-40cd-a079-bb2c83d6fb2e/element/26/clear {"sessionId": "e8d83ee3-7a75-40cd-a079-bb2c83d6fb2e", "id": "26"}
16:56:00.931 DEBUG Finished Request
16:56:00.933 DEBUG POST http://192.168.1.201:4444/wd/hub/session/e8d83ee3-7a75-40cd-a079-bb2c83d6fb2e/file {"sessionId": "e8d83ee3-7a75-40cd-a079-bb2c83d6fb2e", "id": "26", "file": "UEsDBBQAAAAIAGJU9Uhr90YTkgoAAF5CAAABAAAAMe1bbW/bOBL+HP8Kwf5g+dZRGvd6iwtOi3Ob\n9tBDui2SvuCQBAQtMTa7suQVqdTeX398lUhJfpHk7uWA+kMiUjMPh8OZ4QwlDR7SZOkEyXKZxCCj\nOMIUI+JdYUIvIYUOXq6SlDq63asnX6BohVJNnCIY0iTEAZXkaTJLqBfhWQpTTv0ywxF9G2ty1ezl\n3PqK0BTH87wfxmGy1C2KlzldQnKOTX4ZQooEkRBBt5yD747FECGK9KTlLOAKa6oomc9RKm/eoAjF\nOFtOrsQsN5qm3F9P/XpNUUxwEpNtfAVFj/0GDvHLFO6oNyBoUukvODmFk6WR4zv9BaWri7Oz858n\n3vnfvL96538/v5hMJmfT8F8Qx+/mS3r25fH36NNiEX96DOM/vK9k1edDBxEkxHnFlpiiD2kSZgF1\nRxe93kmIHhwAcIwpAC5B0QPrPTkZkIwZhvuSL947GEOmsLG46eW0I0ZG0w2nPhlYwIxmBgkiANw+\nu/dscE79gNlUIVs2n5mAt4J04X1NcOzqBpwR/j9vhzjl5C4AnBOA0Wg8fD/7igJ6jVbJU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:00.944 DEBUG POST http://192.168.1.201:4444/wd/hub/session/e8d83ee3-7a75-40cd-a079-bb2c83d6fb2e/element/26/value {"sessionId": "e8d83ee3-7a75-40cd-a079-bb2c83d6fb2e", "id": "26", "value": ["/", "t", "m", "p", "/", "e", "8", "d", "8", "3", "e", "e", "3", "-", "7", "a", "7", "5", "-", "4", "0", "c", "d", "-", "a", "0", "7", "9", "-", "b", "b", "2", "c", "8", "3", "d", "6", "f", "b", "2", "e", "/", "u", "p", "l", "o", "a", "d", "6", "0", "5", "4", "3", "8", "4", "5", "8", "0", "2", "8", "1", "8", "0", "7", "9", "3", "f", "i", "l", "e", "/", "1"]}"

Bruteforce Attack Project

I am creating a simple 'Bruteforce attack' for a project (not school related). Could someone please tell me which part of the code is wrong to get these errors.
The Code:
#include <string>
using namespace std;
//Password array
std::string passwordArray;
//Lowercare character array
std::string lower = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
//Uppercase character array
std::string upper = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
//Digits array
std::string digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
private void setupCharArray()
{
if (ckhLower.Checked)
{
characterArray.AddRange(lower);
}
if (chkUpper.Checked)
{
characterArray.AddRange(upper);
}
if (chkDigits.Checked)
{
characterArray.AddRange(digits);
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
brute();
}
so I try to compile this code using MinGW
g++ bruteforce.cpp -o bruteforce.exe
and I get these error messages
c:\Users\Lotty Playle\Documents>g++ bruteforce.cpp -o bruteforce.exe
bruteforce.cpp:7:66: error: in C++98 'lower' must be initialized by constructor,
not by '{...}'
bruteforce.cpp:7:66: error: could not convert '{"a", "b", "c", "d", "e", "f", "g
", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w
", "x", "y", "z"}' from '<brace-enclosed initializer list>' to 'std::string {aka
std::basic_string<char>}'
bruteforce.cpp:10:66: error: in C++98 'upper' must be initialized by constructor
, not by '{...}'
bruteforce.cpp:10:66: error: could not convert '{"A", "B", "C", "D", "E", "F", "
G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "
W", "X", "Y", "Z"}' from '<brace-enclosed initializer list>' to 'std::string {ak
a std::basic_string<char>}'
bruteforce.cpp:12:73: error: in C++98 'digits' must be initialized by constructo
r, not by '{...}'
bruteforce.cpp:12:73: error: could not convert '{"0", "1", "2", "3", "4", "5", "
6", "7", "8", "9"}' from '<brace-enclosed initializer list>' to 'std::string {ak
a std::basic_string<char>}'
bruteforce.cpp:14:1: error: expected unqualified-id before 'private'
If anyone knows what I'm doing wrong could they please show me.
L x
An array of strings should look like this: (with [])
std::string lower[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
A std::string of characters would look like this:
std::string lower = "abcdefghijklmnopqrstuvwxyz";
Note that using a bunch of std::strings to represent characters is extremely inefficient. You should use a char array, or just work with ascii arithmetic.