Regular expression for checking postal address field value - regex

I am looking for a regular-expression which can be used to check a postal address field value, with minimum length of 10, containing numbers, and characters as well:
Currently I use this expression:
`\\[a-zA-Z]|\d|.|\s{10,}`
The environment is:
lotus xpages, and the regular expression is stored in properties file within the application design.
<xp:inputText id="address" dojoType="dijit.form.ValidationTextBox" value="#{complaintDocument.address}">
<xp:this.dojoAttributes>
<xp:dojoAttribute name="promptMessage">
<xp:this.value><![CDATA[${javascript:clientData['address']}]]></xp:this.value>
</xp:dojoAttribute>
<xp:dojoAttribute name="placeHolder">
<xp:this.value><![CDATA[${javascript:common['textValueMinimumTenCharacters']}]]></xp:this.value>
</xp:dojoAttribute>
<xp:dojoAttribute name="trim" value="true">
</xp:dojoAttribute>
<xp:dojoAttribute name="regExp">
<xp:this.value><![CDATA[#{javascript:regExp['minimumTenCharacters']}]]></xp:this.value>
</xp:dojoAttribute>
</xp:this.dojoAttributes>
</xp:inputText>
Is there any wat to make the regular expression for this purpose more simple?

I dont thinkt that your regex can be any simpler. Maybe you could use
.{10} > any character, max 10 length
If you want to check if a zipcode is valid you can make a java class that is used to check if the zipcode that was filled in is correct. This class can be a stored in the application scope
<faces-config>
<managed-bean-name>
yourzipcodeclass</managed-bean-name>
<managed-bean-scope>
application</managed-bean-scope>
<managed-bean-class>yourclass</managed-bean-class>
</faces-config>
</faces-config>
Anyways When you want to check if a value is a valid zipcode you should add a method to this class isValidZipCode(String code, String country)
in this method you check depending on the country you have given if the zipcode is correct. How you check it is up to you. You can use a regex for every country or you can use a webservice, or a lookup in a database etc.
You can use this method in a custom validator.

Related

What is a valid AWSPhone? What test values can be used?

The docs say:
The AWSPhone scalar type represents a valid Phone Number. Phone numbers are serialized and deserialized as Strings. Phone numbers provided may be whitespace delimited or hyphenated. The number can specify a country code at the beginning but this is not required.
What determines whether a given string is a valid AWSPhone? In addition, is there any safe way to generate (possibly a large number of) AWSPhone test values that are guaranteed to be valid but assuredly are not in-use phone numbers?
TLDR: You cannot tell the exact rules how the type AWSPhone is validated in AppSync. However, if a value passes the test of the regular expression /^\+?\d[\d\s-]+$/ or validation by libphonenumber-js, then it is likely to be accepted by AppSync.
In the latest AppSync Developer Guide (Oct 6, 2021 UTC), the description was updated to:
A phone number. This value is stored as a string. Phone numbers can contain either spaces or hyphens to separate digit groups. Phone numbers without a country code are assumed to be US/North American numbers adhering to the North American Numbering Plan (NANP).
This doesn't really tell exactly what AppSync expects. E.g. Must country code include + as prefix?
From AWS's public repositories on GitHub, there are hints:
amplify-js datastore util method for frontend validation:
export const isAWSPhone = (val: string): boolean => {
return !!/^\+?\d[\d\s-]+$/.exec(val);
};
amplify-appsync-simulator for amplify CLI mock features:
//...
import { isValidNumber } from 'libphonenumber-js';
//...
const phoneValidator = (ast, options) => {
//...
let isValid = isValidNumber(value, country);
//...
}
Therefore, a value is likely to be accepted by AppSync if it passes the above regex test and validation by libphonenumber-js (or libphonenumber, assuming they work equivalently).
I would have a look at the popular google library for handling phone numbers
https://github.com/google/libphonenumber
libphonenumber-js finds both 5555551212 and 555-555-1212 as invalid. What I have read is area code 555 is not a valid area code. So it would be the ideal number to generate known invalid test phone numbers, as well as a perfect phone number area code to be a known invalid initializer. But alas, dynamodb declares it as invalid.

Validate phone number with Symfony

I want to validate the phone nummer in a form. I would like to check so number and the "(" and ")" char are valid only. So user can fill in +31(0)600000000. The +31 is already preset in the form. The number only is possible with the code below, only how to add the two chars?
Or is there a standaard better way to validate phone number?
#Assert\Length(min = 8, max = 20, minMessage = "min_lenght", maxMessage = "max_lenght")
#Assert\Regex(pattern="/^[0-9]*$/", message="number_only")
If you need a good and robust validator for numbers, with advanced options to valudate, I will advice to use google lib https://github.com/googlei18n/libphonenumber, there is existed symfony2 bundle https://github.com/misd-service-development/phone-number-bundle and you can see there is a assert annotation:
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
/**
* #AssertPhoneNumber
*/
private $phoneNumber;
The regex you need is:
/^\(0\)[0-9]*$
or for the entire number
/^\+31\(0\)[0-9]*$
You can test and play around with your regex here (it also includes auto-generated explanations):
https://www.regex101.com/r/gD0hE5/1

How to find a substring anywhere in a string

This should be easy, but I'm finding it difficult.
I just want to find whether a substring exists anywhere in a string. In my case, whether the name of a website exists in the title of a product.
My code is like this:
#FindNoCase("Amazon.com", "Google Chromecast available at Amazon")#
The above returns a 0 which is correct because the entire substring "Amazon.com" doesn't exist in the main string. But some of it does, namely the "Amazon" part.
How could I achieve what I'm trying to do which is just see if ANY of the substring (at least more than 2 character in length) exists in the main string?
So I need something like FindOneOf() but actually "find at least three of". It should then look at the word "Amazon" in the product title and check if at least 3 characters in the sequence of "Amazon.com" exists. When it sees that "Ama" exists, then it just needs to return a true value. Can it be done using the existing built-in functions somehow?
Update: Very simple solution. I used Left("amazon", 3).
There's a lot of danger in false positives, like if someone was buying the Alabama state flag.
Because of store names that contain spaces, this is a little tricky (Wal Mart is often written with a space).
If your string always contains at [store], you can extract the store name by finding the last at in the sentence and creating a string by chopping off everything else.
Because it looks for occurrences of at only as a whole word, there's no danger with store names such as Beats Audio, or Sam's Meat Shop. I can't think of any any stores with the word at in the name. While that would technically trip it up, there's much lower risk, and you can do a pre-replace on such store names.
<cfset mystring = "Google Chromecast available at Amazon">
<cfset SellerName = REReplaceNoCase(mystring,".*\b(?:at)\b(?!.*\b(?:at)\b)\s*","")>
<cfoutput>Seller: #Sellername#</cfoutput>
You can then do your comparisons much more safely.
Per your comment, If you know all possible patterns, you can still obtain the data if you want to (false positives can either be embarrassing or catastrophic, depending on the action). If you know the stores you're working with, you can use a regex to pull out the string like this
<cfset mystring = "Google Chromecast available at Amazon.co.uk">
<cfset SellerName = REReplaceNoCase(mystring,".*\b((Google|Amazon|Wal[\W]*Mart|E[\W]*bay)(\.[a-z]+)*)\b","\1")>
<cfoutput>Seller: #Sellername#</cfoutput>
The only part you need to update is the pipe-delimited list You might add K-Mart as K[\W]*Mart the [\W]* permits any special character or space so it covers kMart, K-Mart, k*Mart, but not Kwik-E-Mart.
Update #2, per more comments
<cfset mystring = "Google Chromecast available at Toys-R-US">
<cfset SellerNameRE = REReplace(rsProduct.sellername,"[\W]+","[\W]*","ALL")>
<cfset TheSellerName = REReplaceNoCase(mystring,".*\b((#sellernameRE#)(\.[a-z]+)*)\b","\1")>
<cfoutput>Seller: #TheSellername# (#SellerNameRE#)</cfoutput>
This replaces any symbols with the wildcard character so that symbols aren't required so that if something says Wal*Mart, it will still match WalMart.
You could also load a seperate column with "Regex Names" so that you're not doing this each time.
So your table would look something like
SellerID SellerName RegexName
1 Wal-Mart Wal[\W]*Mart
2 Toys-R-US Toys[\W]*R[\W]*US
<cfset mystring = "Google Chromecast available at Toys-R-US">
<cfset TheSellerName = REReplaceNoCase(mystring,".*\b((#rsProduct.RegexName#)(\.[a-z]+)*)\b","\1")>
<cfoutput>Seller: #TheSellername# (#SellerNameRE#)</cfoutput>
Solved it by doing this
#FindNoCase(left("Amazon.com", 3), "Google Chromecast available at Amazon")#
Yes there is potential it won't do what I need in cases where the seller name less than 3 characters long. But I think its rare enough to be ok.

Best way to compare phone numbers using Regex

I have two databases that store phone numbers. The first one stores them with a country code in the format 15555555555 (a US number), and the other can store them in many different formats (ex. (555) 555-5555, 5555555555, 555-555-5555, 555-5555, etc.). When a phone number unsubscribes in one database, I need to unsubscribe all references to it in the other database.
What is the best way to find all instances of phone numbers in the second database that match the number in the first database? I'm using the entity framework. My code right now looks like this:
using (FusionEntities db = new FusionEntities())
{
var communications = db.Communications.Where(x => x.ValueType == 105);
foreach (var com in communications)
{
string sRegexCompare = Regex.Replace(com.Value, "[^0-9]", "");
if (sMobileNumber.Contains(sRegexCompare) && sRegexCompare.Length > 6)
{
var contact = db.Contacts.Where(x => x.ContactID == com.ContactID).FirstOrDefault();
contact.SMSOptOutDate = DateTime.Now;
}
}
}
Right now, my comparison checks to see if the first database contains at least 7 digits from the second database after all non-numeric characters are removed.
Ideally, I want to be able to apply the regex formatting to the point in the code where I get the data from the database. Initially I tried this, but I can't use replace in a LINQ query:
var communications = db.Communications.Where(x => x.ValueType == 105 && sMobileNumber.Contains(Regex.Replace(x.Value, "[^0-9]", "")));
Comparing phone numbers is a bit beyond the capability of regex by design. As you've discovered there are many ways to represent a phone number with and without things like area codes and formatting. Regex is for pattern matching so as you've found using the regex to strip out all formatting and then comparing strings is doable but putting logic into regex which is not what it's for.
I would suggest the first and biggest thing to do is sort out the representation of phone numbers. Since you have database access you might want to look at creating a new field or table to represent a phone number object. Then put your comparison logic in the model.
Yes it's more work but it keeps the code more understandable going forward and helps cleanup crap data.

Search for specific word and extract it into new colomn in Power BI DAX

Atm my data is like a list of sentence like following:
FPTS is our (Case ID)
Column
FPTS-0009: needed help for software update
Helped and customer doing frontend FPTS-0012
This time the customer FPTS-0020 needed refresh data
I want to find the word "FPTS" and extract it's ID and have it inside a new colomn. As you can see the word I need to extract doesn't have the same position all the time! sometimes it shifts What I try to do is extract FPTS-xxxx from above which would be:
FPTS-0009
FPTS-0012
FPTS-0029
would help a lot to find a solution!
If the string is always going to be FPTS-####, you can do the following:
Found_Text = iferror(MID('table'[column], find("FPTS", '8HP_Tatoo_Today_Link'[TEXT_1]), 9), "")
This uses the FIND function, which will return the position in the sentence where "FPTS" is found. Then it uses this in the MID function, which returns the string of characters with a start position and a length. The find is used to define the starting index and then the length is 9.
It is wrapped in an IFERROR, because if the FIND returns nothing(string doesn't contain "FPTS") it will throw an error.