A valid Android package name is described in the AndroidManifest documentation for the package attribute:
The name should be unique. The name may contain uppercase or lowercase letters ('A' through 'Z'), numbers, and underscores ('_'). However, individual package name parts may only start with letters.
See: https://developer.android.com/guide/topics/manifest/manifest-element.html#package
The following regular expression will match a valid Android package name:
java
^([A-Za-z]{1}[A-Za-z\d_]*\.)+[A-Za-z][A-Za-z\d_]*$
Example usage:
java
String regex = "^([A-Za-z]{1}[A-Za-z\\d_]*\\.)+[A-Za-z][A-Za-z\\d_]*$";
List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);
for (PackageInfo packageInfo : packages) {
if (packageInfo.packageName.matches(regex)) {
// valid package name, of course.
}
}
For a detailed explanation of the regex, see: https://regex101.com/r/EAod0W/1