How do I force input to be string only? - powershell

I need the user to input a title of a book.
I need to make sure they input string only.
This is where I'm at so far, any guidance please.
Do { $strTitle = Read-host "Enter the book title"}
while ($strTitle -eq "")

What do you mean by string? alpha characters only?
You could try a regular expressions.
Regular Expression to match only alphabetic characters
http://powershell.com/cs/blogs/tobias/archive/2011/10/27/regular-expressions-are-your-friend-part-1.aspx

Code to check for alpha characters:
Do {
$strTitle = Read-host "Enter the book title"
} until ($strTitle -notMatch "[^[:alpha:]]")

Related

Using 'N in ADO.recordset.filter string

I have a VB6 ADO recordest that contains unicode text and need to filter it using .filter method with unicode text. I tried to use N' function but it does not work when I use it in .Filter text
Rs.filter = "Name = 'Apple'" 'OK
Rs.Filter = "Name = N'英語'" 'Not working for filter text
How I should wrap the text or I need to use another function?

Split a string based on "|" character in PowerShell

I have a string variable in PowerShell which contains the value:
NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8
I am attempting to get the beginning portion of that string into it's own variable by identifying the index of the "|" character and using a substring function to extract the first portion of the string, in this case "NFP". I am not sure how to escape the "|" so I can use it properly. It doesn't seem to recognize it at all. My latest attempt is as follows:
$PolicyManual = $Item["PolicyManual"]
write-host $PolicyManual #Displays NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8
if ($PolicyManual.Contains([regex]::escape("|"))) {
$PolcyManual = $PolicyManual.Substring(0, $PolicyManual.IndexOf([regex]::escape("|")))
}
I'm sure this is simple, but I can't figure out how to make it work. Can anyone offer assistance to a PowerShell novice?
Thanks.
The problem is that .contains method doesn't know about regex and you are never entering the if condition because of this. When you do [regex]::escape("|"), the method is looking for a literal \|.
Try this instead:
$PolicyManual = "NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8"
if ($PolicyManual.Contains('|')) {
$element0, $element1 = $PolicyManual.Split('|')
$element0 #=> NFP
$element1 #=> 8dc3b47a-48eb-4696-abe2-48729beb63c8
}

Regular Expression

I need to extract few values from below string with Powershell Regex.
Request ID = 1234 { andquot;EMOandquot;: andquot;123456-Uandquot;, andquot;Terminated Accountandquot;: andquot;Test Userandquot;, andquot;Descriptionandquot;: andquot;andquot;, andquot;Last Dayandquot;: andquot;2019-06-26andquot;, andquot;Terminated User Mailandquot;: andquot;Test.User#gmail.comandquot; } Location : UK ,London
I Need to get Test.User#gmail.com, Test User and 2019-06-26. Please help me to get powershell regex for getting these values from above string.
Thank you.
I Tried below -
$description = "Request ID = 1234 { andquot;EMOandquot;: andquot;123456-Uandquot;, andquot;Terminated Accountandquot;: andquot;Test Userandquot;, andquot;Descriptionandquot;: andquot;andquot;, andquot;Last Dayandquot;: andquot;2019-06-26andquot;, andquot;Terminated User Mailandquot;: andquot;Test.User#gmail.comandquot; } Location : UK ,London"
$formatdesc = $description -replace ' ?(and)?quot;','"'
$formatdesc
Request ID = 1234 {"EMO":"123456-U","Terminated Account":"Test User","Description":"","Last Day":"2019-06-26","Terminated User Mail":"Test.User#gmail.com" } Location : UK ,London
With above how would I have extract Terminated User Mail, Terminated Account and Last Day the values are not static they are dynamic. Please help.
Break down the pattern logically you are looking to find first. It looks like you are looking for: Test.User#gmail.com Test User - use a simple -match e.g. $Myvariablename = [Your string] -match 'Test.User#gmail.com'
2019-06-26: For a date like this, break it down to its parts so that's 4 digits, a hyphen, 2 digits, a hyphen and then 2 digits so that (quickly and therefore not perfect without testing) comes out to a -match like $Myvariablename = [Your string] -match '^\d{4}-\d{2}-\d{2}'

How do I tell my script to go back to a certain point of the script?

Here is my script so far:
$BookTitle = Read-Host "Enter Book Title"
If ($BookTitle -eq "")
{
[System.Media.SystemSounds]::Beep.Play();
Write-Host "Please enter a book title"
}
Else
{
Write-Host "" ;
Write-Host "Book title is: $BookTitle"
}
What I want to do is if a user enters a book title that is null, I want the script to execute the error code block:
{
[System.Media.SystemSounds]::Beep.Play();
Write-Host "Please enter a book title"
}
and jump back to the beginning of this particular segment:
$BookTitle = Read-Host "Enter Book Title"
Instead of continuing on with the rest of the script. I don't want it to jump to the beginning of the entire script because I plan on using this kind of logic frequently within the script.
I'm not sure how to do it. I've tried using loops instead like Do...While, Do...Until, While, but I keep getting infinite loops.
So my question is, what am I doing wrong? Should I be using loops instead of conditional statements? A mixture of both?
Do {
$BookTitle = read-host "enter booktitle.."
[System.Media.SystemSounds]::Beep.Play();
} while ($BookTitle -eq "")
$BookTitle

NSRegularExpression to remove white space

I need a little kickstart on regex on the iPhone. Actually I am dealing with UITextField.text. If the value of the text is empty and if the value already exist, I can able to deal it. But, if the value is simply white spaces, I do not want to use it. So, if the value is like " " or " folder", I want the value to be "" and "folder" respectively.
I planned to use NSRegularExpression to remove the white space and went through the documents. But it was little confusing. So, help me to come out of the problem of removing white space from the given string. Thank you in advance.
Edit: you need to trim string, so no regular expression is needed, simply use:
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
The regexp would be:
/\s+/g
You would then replace that with ""
How you do that through iOS syntax for the replacement I don't know, but that's the regExp for it all :)
A more practical question is, How to trim and condense white-spaces,
let text: String? = " I don't know you ! " // expected result: "I don't know you!"
let charSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
if let trimmedText = text?.componentsSeparatedByCharactersInSet(charSet).filter({!$0.isEmpty}).joinWithSeparator(" ") {
print(trimmedText) // I don't know you!
}