$re = '/^arn:(aws|aws-cn|aws-us-gov):([a-zA-Z0-9-]+):([a-zA-Z0-9-]*):(\d{12}|):([^:/]+)([:/].*)?$/m';
$str = 'AWS
arn:aws:s3:::my-s3-bucket
arn:aws:iam::123456789012:user/JohnDoe
arn:aws:dynamodb:us-east-1:123456789012:table/UserTable
arn:aws:lambda:us-west-2:123456789012:function:ProcessData
arn:aws:ec2:us-east-1:123456789012:instance/i-0abcd1234efgh5678
GOOGLE CLOUD
projects/my-project-id/buckets/my-storage-bucket
projects/my-project-id/zones/us-central1-a/instances/instance-1
projects/my-project-id/topics/my-pubsub-topic
projects/my-project-id/datasets/my_bigquery_dataset
projects/my-project-id/instances/my-cloud-sql-instance
AZURE
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.KeyVault/vaults/MyKeyVault
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageacct
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/MyVM
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyAppService
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.DocumentDB/databaseAccounts/MyCosmosDB';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php