Test of a regex (substring) occurs anywhere in any of the items in an array of strings - ruby-on-rails-3

My spec wants to test if a certain substring occurs withing any entry of an array of strings.
p #banner.errors.messages[:base] #=> ["Specify a leader text or an image, not both"]
All my spec really wants to know, is whether or not the "not both" string occurs in any of the array-items in there.
#banner.errors.messages[:base].should include(/not both/)
fails, because "not both" is not included in ["Specify a leader text or an image, not both"]
Note: When I test against the literal string (should include("Specify...both"), things work. But that seems dirty to me. Such user-faced texts are not critical for the test to pass; and such texts will change: every time the error message is changed, I will need to update my tests.

Maybe like this?
#banner.errors.messages[:base].join.should match(/not both/)
But note that there is a edge case where the match might be over two or more lines, e.g. a line ending with "not " and the next line is " both".

Related

How to make a Password Validator in Scratch

So I am trying to make a password validator in Scratch where it asks the user to input an answer and then it puts the answer through some criterias and then outputs if it is a valid password or not. The criterias are:
Has at least 8 characters,
Has at least one uppercase letter,
Has at least one lowercase letter,
Has at least one number,
Has at least one special character,
Must contain less than 18 characters.
I tried to make a list first with all the different characters and check if the password contained them, but it doesn't actually work. I looked all over the internet for help on this but no one seems to have done it. The Scratch Wiki does have some stuff about case sensitivity but I haven't really been able to implement it. I really need help and I have been trying for a while now. Thanks.
If you just check if the password contains the list, it will only work if it has every single character of the list in order. If you want to make sure it contains each check, you're probably going to have to make a system that checks each letter for every check, which is a little complex.
Check if <lowercase letter/whatever check> contains(letter(text reading #) of (password))
If it passes this check, continue to the next check and set text reading # to 1. Otherwise, change text reading # by 1.
I assume you'll know how to code this properly, but I just partially phrased in the way a normal human would.
This will repeat until either it reaches the end of the password or it passes the check. it will then do this again, but for a different check. It's hard to explain in text, and this is my first answer, but I hope it helps.
You have to use the operators "contains", "length of" and > operators, from the end of the class. Combine "contains", "or" and "and".

Regex matching sequence of characters

I have a test string such as: The Sun and the Moon together, forever
I want to be able to type a few characters or words and be able to match this string if the characters appear in the correct sequence together, even if there are missing words. For example, the following search word(s) should all match against this string:
The Moon
Sun tog
Tsmoon
The get ever
What regex pattern should I be using for this? I should add that the supplied test strings are going to be dynamic within an app, and so I'd like to be able to use a pattern based on the search string.
From your example Tsmoon you show partial words (T), ignoring case (s, m) and allow anything between each entered character. So as a first attempt you can:
Set the ignore case option
Between each chapter input insert the regular expression to match zero or more of anything. You can choose whether to match the shortest or longest run.
Try that, reading the documentation for NSRegularExpression if you're stuck, and see how it goes. If you get stuck ask a new question showing your code and the RE constructed and explain what happens/doesn't work as expected.
HTH

Replacing first and last character of every word using REGEXP_REPLACE

My question is somewhat specific, I'm not using any kind of code compiler to achieve the result in the title, I am using a IRC Client that allows the use of "Quirks" so the users can have specific mannerisms when chatting, like starting every word with an uppercase, or changing every "s" into a "2".
Problem is that I can't see the whole code so even though I'm not familiar with REGEXP_REPLACE it makes things harder to learn.
The client simplifies the whole coding process, here's a screenshot of the
interface
Filling the text boxes with "^(\w)" and "upper(\1)" respectively makes the first character capitalized, "(\w)$" and "upper(\1)" does the same with the last character.
I've discovered that "\b(\w)" will uppercase the first character of every word, i've tried "\b(\w)%" for the last character but it didn't work, as there is some syntax error, probably...
So, how do I get every last character capitalized?
1:

Preventing VB.NET's multiline/verbatim string from destroying your entire code file?

Let's say you have a module that's several hundreds of lines long. At the very top of your code file, you go to start up a string, so you type a quote. Total wreckage ensues as the string remains unterminated for a time, causing everything within your entire code file to be subject to erratic encapsulation by your string (see image for actual example of all the errors generated). No big deal, right? You just finish your string and all the errors will go away. While true, you may find the IDE has had its way with other strings in your document. For example, these lines...
oLog.writeLogFile("Starting System Update and Version Update ")
oLog.writeLogFile("Starting Script for Fetching Data from Source to Dest")
...get changed to this:
oLog.writeLogFile("Starting System Update And Version Update ")
oLog.writeLogFile("Starting Script For Fetching Data from Source To Dest")
Notice how and changes to And, for to For, and to to To. What's happening here is that, as other strings in the document become... eh... "destrung"... so some of the words that were once part of a string are now interpreted as keywords by the IDE. Because it's VB, it modifies capitalization automatically. When you finally terminate your string, all the other strings further down in the document become properly terminated as well, but the jarring effects still remain.
Is there a way to prevent this from occurring?
Why not first type a double ", then return in between them and start typing your string? I do it all the time to prevent this. I find that the short delay in between typing your first " and the moment the IDE starts capitalizing keywords is long enough for me to (remember to) type the second ".

VBA for each loop locked when I revert revisions

I want to get both the revised and original text from a document. I do it this way:
Set wrdDoc = wrdApp.Documents.Open(fileName)
For each sent in wrdDoc.Sentences
if sent.Revisions.Count >=0 then
after=sent.text
sent.Revisions.RejectAll
before=sent.text
SaveRev(before,after)
End if
next
Now that would be fine, except that malformed sentences like
This is one sentence.This is another.
Will get parsed in a weird way. First, there will be this one: "This is one sentence.", then this one with both "This is one sentence.This is another."
What happens when there are revisions there? The first iteration will revert revisions on the first sentence, then the second iteration will not 'see' that revised portion.
Bottom line is, the first iteration will get both versions of the first sentence, and the second iteration will get only the original version of the first sentence (while getting both versions from the second sentence).
Let me clarify:
Let's say I had the original
We started with this sentence.And this sentence.
And it was revised to
We ended with this sentence.And this other sentence.
First iteration will result in
Before: We started with this sentence.
After: We ended with this sentence.
But second iteration will have
Before: We ended with this sentence.And this sentence.
After: We ended with this sentence.And this other sentence.
Well, what I did was alter the logic, undoing the revision reversion:
Set wrdDoc = wrdApp.Documents.Open(fileName)
For each sent in wrdDoc.Sentences
if sent.Revisions.Count >=0 then
wrdDoc.Undo
after=sent.text
sent.Revisions.RejectAll
before=sent.text
SaveRev(before,after)
End if
next
I like this because I end up with an unaltered document (except for the last sentence).
The thing is, doing this puts the macro in an infinite loop at one specific sentence.
I have no idea of the mechanics of the for each, I have no clue what is causing it to hang. Obviously altering the collection is messing up the loop, but I don't understand why.
I could loop for i=0 to wrdDoc.Sentences.Count, but I think that will make me skip sentences for the same reasons I'm repeating one now, and I cannot risk it (even if I test OK, I have to be sure it will never happen).
So the question is (are):
Can any one help me figuring out why it's locking on a sentence,
Is there a better way of doing this?
How can I solve it while making sure not to skip sentences.
Thank you very much!
PS: I can provide sample documents, let me know if it's needed (maybe what I'm doing wrong is already clear to someone, and I'd have to make the samples as I cannot share the documents I'm working on).
--EDIT--
Ok so this is where it's hanging, only on the 32nd file.
It doesn't hang on a sentence, it actually does a few at the start of the document, then goes back to the beginning.
I previously encountered the same error, but it looped in a single sentence, and didn't go back to the beginning. I think it's the same issue. I'll try to reproduce original and revised versions here.
Originalversion
MAIN TITLE
Measurement of some variable
1 REQUIRED TOOLS
1.1 Special tools
NOTe:
Some note about the procedure (unaltered by revision)
Equipment name (carrier returned line)
(english) assemply with Equipment PN
Kit
Equipment name (carrier returned line)
(english) assemply with (Another) Equipment PN
Kit
Document continues...
There are 2 equipment entries before it restarts the loop.
Revision consisted of inserting the document number, some First Letter of the Word caps, and changing the order between Equipment PN and "Kit".
Revised version
ducument number
MAIN TITLE
Measurement of Some Variable
1 REQUIRED TOOLS
1.1 Special Tools
NOTe:
Some note about the procedure (unaltered by revision)
Equipment name (carrier returned line)
(english) assemply with kit
Equipment PN
Equipment name (carrier returned line)
(english) assemply with kit
(Another) Equipment PN
Document continues...
Recorded original/revison pairs were:
Original..................................Revised
{Empty}...................................Document number
Measurement of some variable..............Measurement of Some Variable
Special tools............................Special Tools
(english) assemply with..................(english) assemply with kit
(english) assemply with..................(english) assemply with kit
Then it starts again, recording the same entries until I break.
I don't see the sentences overlapping I talked about, but there was a line break insertion on the revision.
Thanks!
Enumerable objects should not be altered during the enumeration or bad things can happen (what depends on the type of collection).
My guess is that the revision/undo process, combined with the wonky sentence, is causing the Sentences enumerable to change.
You should prepare your own collection first, to see if that makes a difference. Simply try Set sents = New Collection: For Each sent in wrdDoc.Sentences: sents.Add sent: Next then use sents for your main For Each loop.