diff --git a/apps/files_sharing/ajax/getitem.php b/apps/files_sharing/ajax/getitem.php
index ff6c29b6a0a08ad08e0c704dad298f42c53360ee..06a80102dec982bf7c3d6375191d63d25c05e8f3 100644
--- a/apps/files_sharing/ajax/getitem.php
+++ b/apps/files_sharing/ajax/getitem.php
@@ -22,8 +22,13 @@ while ($path != $userDirectory) {
 				}
 			} else {
 				// Check if uid_shared_with is a group
-				if (($pos = strpos($uid_shared_with, '@')) !== false) {
+				$pos = strrpos($uid_shared_with, '@');
+				if ($pos !== false) {
 					$gid = substr($uid_shared_with, $pos + 1);
+				} else {
+					$gid = false;
+				}
+				if ($gid && OC_Group::groupExists($gid)) {
 					// Include users in the group so the users can be removed from the list of people to share with
 					if ($path == $source) {
 						$group = array(array('gid' => $gid, 'permissions' => $rows[$i]['permissions'], 'users' => OC_Group::usersInGroup($gid), 'parentFolder' => false));
diff --git a/apps/files_sharing/lib_share.php b/apps/files_sharing/lib_share.php
index 0237acfc1ac3b06285f01159fadd2e0440c94aba..3bedd9bebc80f138e0aeddb2f659c9c248510708 100644
--- a/apps/files_sharing/lib_share.php
+++ b/apps/files_sharing/lib_share.php
@@ -179,7 +179,7 @@ class OC_Share {
 				$uid_shared_with = OC_Group::usersInGroup($uid_shared_with);
 				// Remove the owner from the list of users in the group
 				$uid_shared_with = array_diff($uid_shared_with, array(OCP\USER::getUser()));
-			} else if ($uid = strstr($uid_shared_with, '@', true)) {
+			} else if ($uid = strrchr($uid_shared_with, '@', true)) {
 				$uid_shared_with = array($uid);
 			} else {
 				$uid_shared_with = array($uid_shared_with);