Today, pervasive systems have become an inseparable part of computer science and engineering. These systems provide automated connection with remote access and seamless transmission of biological and other data upon request. The health domain is one of the most important application of these systems. Moreover, heart is the most important part of human body and cardiac diseases are the second leading cause of death. Therefore, different tools and methods have been invented for the rapid investigation and early detection of cardiac diseases and the cardiac operations. These methods aim to obtain structural and operational information about the heart. Any changes in the form of cardiac signals can indicate a disease or abnormal behavior of the heart. Therefore, early detection of these changes can be significant to prevent and treat cardiac diseases. This paper proposes a method to detect atrial arrhythmia, which is one of the most common cardiac anomalies. The proposed approach can be generalized to detect other arrhythmia disorders. The proposed method models an arrhythmia by a regular expression. After removing the noise and performing the proposed segmentation algorithm, the input signal of the patient is transformed into a character string in which each character represents an ECG signal component. Moreover, a LCS-based tree comparison algorithm is proposed to detect any disorder in the input signal. The proposed algorithm can be used in cell phones or wearable devices. Different experiments on MIT-BIH arrhythmia dataset show the efficiency of the segmentation method and the detection algorithm in comparison to conventional approaches.