I have tried matching Phone numbers with the regular expressions provided by Android in Patterns.Phone,this matches a lot of things that are not phone numbers.I have also tried using:
(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?
However,I found that the Test was not successfull for all the inputs.I would like to validate the following inputs using a regular expression:
67450450
+9144-27444444
27444444
27470570
+12142261347
+61406366180
0891 2577456
2577456
+91 9550461668
9550461668
03-1234567
1860 425 3330
Basically any nymber format supported here:WTND
you can use the following code to check phone #:
private boolean validPhone(String phone) {
Pattern pattern = Patterns.PHONE;
return pattern.matcher(phone).matches();
}
if(validPhone("67450450")){
Toast.makeText(this,"The phone number is valid");
}
else
{
Toast.makeText(this,"The phone number is not valid");
}
This isn't clean/efficient, just thrown together to match your sample data:
\b\d{7,10}|\+\d{4}-\d{8}|\+\d{11}|\d{4}\s\d{7}|\+\d{2}\s\d{10}|\d{2}-\d{7}|\d{4}\s\d{3}\s\d{4}\b
Related
I'm making a special file saving system for IOS and possibly mac.
My program will basically allow the user to write the name of the file, but before I do that I need to check for illegal characters that exist for IOS, Windows, and Android since they will all be probably be passed into another system via the user's own decision (Like through email or USB for example).
In general if you try to bring any file with an illegal "name" into another system, the file will either become corrupted or not be passed into the system at all.
The problem is with my code is that it ONLY works if there is ONE character in the string in total. If the former situation, the regex checker doesn't detect anything regardless of where or how many illegal characters are there.
This is my code.
//Validates the string for illegal file name characters
//https://stackoverflow.com/questions/14635391/java-function-to-return-if-string-contains-illegal-characters
func hasIllegalCharacters(locationNameString: String) -> Bool{
//do {
//Pattern pattern = Pattern.compile("[\\\\/:*?\"<>|]");
let illegalRegEx = "[~##*+%{}<>\\[\\]|\"\\_^]"
//let ipAddressRegEx = "|\\?*<\":>+[]/'"
let trimmedString = locationNameString.trimmingCharacters(in: .whitespaces)
let validateName = NSPredicate(format:"SELF MATCHES %#", illegalRegEx)
let isIllegal = validateName.evaluate(with: trimmedString)
if(isIllegal){
print("has illegal chars")
}
else{
print("no illegal detected")
}
return isIllegal
}
extension String {
var containsSpecialCharacter: Bool {
let regex = ".*[^A-Za-z0-9].*"
let testString = NSPredicate(format:"SELF MATCHES %#", regex)
return testString.evaluate(with: self)
}
}
Regex makes lists all non illegal characters and then testString tests if the if the string matches with the characters in regex. It will return True or False
I am trying to read from a Bloodsugar Meter using NFC, right now on an Android, haven't tried iOS yet (don't have a phone with NFC).
I am using react-native-nfc-manager as library and the example that comes with it:
https://github.com/whitedogg13/react-native-nfc-manager
I am receiving this tag:
{ "techTypes":["android.nfc.tech.NfcV","android.nfc.tech.NdefFormatable"], "id":"87C5280D002602E0"}
I can see that NfcV is covered in this library, but how do I read it as that type?
I am following the example so I haven't set anything in my manifest or my build.gradle. I have linked it and it is working, but im missing the last part it seems.
By following the example it looks like I am supposed to use a method like this:
_parseText = (tag) => {
try {
if (Ndef.isType(tag.ndefMessage[0], Ndef.TNF_WELL_KNOWN, Ndef.RTD_TEXT)) {
return Ndef.text.decodePayload(tag.ndefMessage[0].payload);
}
} catch (e) {
console.log(e);
}
return null;
}
But my tag doesn't have a ndefMessage[0].
Since your tag does not contain Ndef in its techTypes list, it does not contain an NDEF message. Consequently, you won't be able to read any such message. As your "tag" is a blood sugar meter, I assume that it's not even expected to contain an NDEF message.
Instead, you will have to find out what commands the blood sugar meter actually supports (probably it will support the ISO/IEC 15693 READ SINGLE BLOCK command (see here). In order to send such low-level commands, you will need to use the Generic NfcTech API by requesting the tag technology:
NfcManager.requestTechnology(NfcTech.NfcV)
You can then use the transceive method to exchange arbitrary commands:
NfcManager.transceive(...)
I'm new to android development. I am trying to make an SMS app. Everything works fine already except for phone number formatting. Say for example, I live in the Philippines and I got 2 different SMS from the same number.
First SMS address: +639123456789
Second SMS address: 09123456789
+639123456789 must equal to 09123456789
Or Swiss number +41446681800 must equal to 0446681800
Now how can I format either of these addresses that they will match. String manipulation will work but it's limited for Philippines only. I found this libphonenumber but I have no idea how to use it on my current project. Sorry for being noob. Any help would be much appreciated.
Here you can find an example for libphonenumber lib.
Using this library you can convert those numbers into international format, after you can match the numbers and if you want, you can get the country code too.
internationalFormatMobileNumber = phoneUtil.format(yourNumber, PhoneNumberFormat.INTERNATIONAL);
If you know the country for which you want to do it, you can use Google's open source library https://github.com/googlei18n/libphonenumber . Here is how you can format it:
String numberStr = "8885551234"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber numberProto = phoneUtil.parse(numberStr, "US");
//Since you know the country you can format it as follows:
System.out.println(phoneUtil.format(numberProto, PhoneNumberFormat.NATIONAL));
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
I am using Asian characters in my android app, I learned already that some characters are not displayable because they are not supported by the system's fonts. I query a database with Asian characters and I often retrieve signs which are not displayable. These cases are usually not a problem for my application, but are irritating to the user if I display not supported characters.
Is there something like a test of a sign for displayability in Android?
This will be helpful
public boolean isPrintable( char c ) {
Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
return (!Character.isISOControl(c)) &&
block != null &&
block != Character.UnicodeBlock.SPECIALS;
}
Please refer printable char in java
Hi all bug reporting for your information. link
Problem details:
The Code - wifiManager.getConnectionInfo().getSSID()
The above code to returns the current SSID, it is returning the current SSID with extra quotations around it.
For eg. the SSID internet is returned as "internet".
This is only seen on Jelly bean 4.2 using device Nexus 7.
This bug is causing errors in our app as we compare the current SSID with the SSID that we are trying to connect too.
The code wifiManager.getScanResults(); however still returns all SSID's without extra quotation marks.
this is not a bug and behavior is correct as per documentation at http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getSSID()
The so-called bug apparently was in pre 4.2 devices, because they didn't return it with "" enclosure.
Aiden's method looks good to me in the current state of confusion left by Android. However, being theoritically correct would just require
if (ssid.startsWith("\"") && ssid.endsWith("\"")){
ssid = ssid.substring(1, ssid.length()-1);
}
This regular expression is quite neat:
String ssid = wi.getSSID().replaceAll("^\"(.*)\"$", "$1");
Just for the notes
Edit °1 (as per question in the comment):
The issue that OP describes is, that on some devices the SSID returned by getSSID() is enclosed in "" whereas it is not on other devices. E.g. on some devices the SSID is "MY_WIFI" and on others it is MY_WIFI - or spoken in Java code: "\"MY_WIFI\"" and "MY_WIFI".
In order to to unify both results I proposed to remove the " at start and end - only there, because " is a legal character inside the SSID. In the regular expression above
^ means from start
$ means at end
\" means " (escaped)
.* means any number of characters
(...) means a capturing group, that can be referred by $1
So the whole expression means: replace "<something>" by <something> where $1 = <something>.
If there is no " at end/start, the regular expression doesn't match and nothing is replaced.
See Java Pattern class for more details.
For the mean time this is how I am getting around it, although its not great it will fix the issue.
public String removeQuotationsInCurrentSSIDForJellyBean(String ssid){
int deviceVersion= Build.VERSION.SDK_INT;
if (deviceVersion >= 17){
if (ssid.startsWith("\"") && ssid.endsWith("\"")){
ssid = ssid.substring(1, ssid.length()-1);
}
}
return ssid;
}
Two very simple variants:
string = string.replaceAll("^\" | \"$", "");
and
string = string.substring(1, string.length() - 1);
Faced the same problem! Used this technique which is backwards compatible:
if (suppliedSSID.equals(connectionInfo.getSSID()) || ("\"" + suppliedSSID + "\"").equals(connectionInfo.getSSID()) { DO SOMETHING }