Regex to identify illegal characters in a perso-arabic database


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex to identify illegal characters in a perso-arabic database
# 1  
Old 08-26-2017
Regex to identify illegal characters in a perso-arabic database

I am working on Sindhi: a perso-Arabic script and since it shares the Unicode-block with over 400 other languages, quite often the database contains characters which are not wanted: illegal characters.
I have identified the character set of Sindhi which is given below:
For clarity's sake, each character is demarcated by an apostrophe followed by a comma
Code:
['ا','ب','ٻ','پ','ڀ','ت','ٺ','ٽ','ث','ٿ','ف','ڦ','گ','ڳ','ڱ','ک','ي','د','ذ','ڌ','ڏ','ڊ','ڍ','ح','ج','ڄ','ڃ','چ','ڇ','خ','ع','غ','ر','ڙ','م','ن','ل','س','ش','و','ق','ص','ض','ڻ','ط','ظ','ھ','جھ','گھ','ڪ','ء','ه','آ']

I wrote a regex in Unix to identify all words where the Sindhi character set does not exist.
Code:
^[^ابٻپڀتٺٽثٿفڦگڳڱکيدذڌڏڊڍحجڄڃچڇخعغرڙمنلسشوقصضڻطظھجھگھڪءهآ]+$

The syntax being: find all strings where the given characters are not found.
However the regex does not work. What went wrong? Do I need to put a comma after every character ?
I am giving below a small sample database where there are words having both legal and illegal characters
Code:
آبادلاهورپشاورڪوئٽه
اپریل
ای
ایڈریس
بيورو
جائے
جنوری
جوابدارشاهد
خانوں
دتجايا
دیں
روحناي
سنڌسماءَچار
سے
ضروری
فروری
مئی
منقتل
میل
نيشنلاسلام
ویب*
پاڪستانسنڌمقامي
پوڻان
ڊ
کریں
کیا
ڪراچي-حيدرآباد
گرميت
گھنٹے
گیا

Any help given would be greatly appreciated. Many thanks in advance
# 2  
Old 08-26-2017
Try egrep or equivalent (extended regex or pcre), use alternation. I am not going to change locale here so example:
Code:
if [  grep -e  '(a|b|c|d|e|f)' $search_string ] ; then
  echo 'bad character'
fi

Where a b c d e f are each a bad character so presence of any or all is a bad character error.
# 3  
Old 08-26-2017
Hi gimley,
Your problem statement is not clear to me. Are you trying to identify a file that only contains Sindhi characters (plus <newline> characters), a file that contains one or more characters that are not Sindhi characters and not <newline> characters, a list of lines that only contains Sindhi characters (plus <newline> characters), a list of lines that contains one or more characters that are not Sindhi characters and not <newline> characters, a list of words that only contains Sindhi characters, or a list of words that contains one or more characters that are not Sindhi characters.

Hi Jim,
The standard way to invoke grep with an ERE is:
Code:
grep -E 'extended_regular_expression' file_pathname

not:
Code:
grep -e 'extended_regular_expression' string_to_be_matched

# 4  
Old 08-26-2017
Wildly guessing on what your after, regretting that there's no output sample to test against, and hoping that UTF-8 will cover all your needs and will be correctly handled by the tools, I came up with

Code:
echo "[^'ا','ب','ٻ','پ','ڀ','ت','ٺ','ٽ','ث','ٿ','ف','ڦ','گ','ڳ','ڱ','ک','ي','د','ذ','ڌ','ڏ','ڊ','ڍ','ح','ج','ڄ','ڃ','چ','ڇ','خ','ع','غ','ر','ڙ','م','ن','ل','س','ش','و','ق','ص','ض','ڻ','ط','ظ','ھ','جھ','گھ','ڪ','ء','ه','آ']" | tr -d "'," | grep -f- file

, which greatly reduces the file's line count (i.e. identifies lines with non-Sindhi chars, lets all-Sindhi lines pass), as does
Code:
grep "[^ابٻپڀتٺٽثٿفڦگڳڱکيدذڌڏڊڍحجڄڃچڇخعغرڙمنلسشوقصضڻطظھجھگھڪءهآ]" file

Is that close to what you need?

If there are "illegal" (why illegal, by the way?) characters in the DB, someone must have put them in. Mayhap an upfront discriminator would be helpful here?

Last edited by RudiC; 08-26-2017 at 05:35 AM..
# 5  
Old 08-26-2017
The + special meaning is in ERE that is grep -E or egrep.
Also you have a ^ negation too many.
Code:
egrep '^[ابٻپڀتٺٽثٿفڦگڳڱکيدذڌڏڊڍحجڄڃچڇخعغرڙمنلسشوقصضڻطظھجھگھڪءهآ]+$'

# 6  
Old 08-27-2017
Sorry for the delay in responding but I was unwell and could not respond. I would like to thank all who took the trouble to answer my query.
The issue is that the Arabic code block is huge and has characters that look alike. Very often data entry operators use a character /characters which are not part of the character set of a language [in this case Sindhi] and these are what we term as illegal. When these invalid/illegal characters are part of a dictionary, the results are disastrous, especially in storage and Natural Language processing.
This is the reason of my query, I tried the solutions provided and they all work and I am really thankful to all for your help.
Why I needed a simple regex was that my text processors: Ultraedit and Notepad++ both support regexes in perl and Unix and instead of "grepping" the strings, a macro based on a regex would help me identify all such invalid strings. I am still curious why the regex did not work. Any light on the same would really help.
Many thanks once again.
# 7  
Old 08-27-2017
As I said, if a-z are allowed then you need
^[a-z]+$ (or [^a-z]ensure all are allowed, or there is not any illegal)
but not ^[^a-z]+$ (ensure there is only illegal)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Regex to identify pattern

Hi In a file I have string in multiple lines. Like below: <?=test.getObjectName("L", "testTBL","D") ?> <?=test.getObjectName("L", "testTBL","testDB", "D") ?> I want to use regex to search for the pattern "<?=test.getObjectName...?>" If the parenthesis has 3 parameters then return 2nd... (5 Replies)
Discussion started by: dashing201
5 Replies

2. Shell Programming and Scripting

Regex to identify unique words in a dictionary database

Hello, I have a dictionary which I am building for the Open Source Community. The data structure is as under HEADWORD=PARTOFSPEECH=ENGLISH MEANING as shown in the example below अ=m=Prefix signifying negation. अँहँ=ind=Interjection expressing disapprobation. अं=int=An interjection... (2 Replies)
Discussion started by: gimley
2 Replies

3. Shell Programming and Scripting

Writing a clustering concordance for a Perso-Arabic script

I am working on a database of a language using Arabic Script. One of the major issues is that the shape of the characters changes according to their initial, medial or final positioning. Another major issue is that of the clustering of vowels within the word: the clustering changes totally the... (9 Replies)
Discussion started by: gimley
9 Replies

4. Shell Programming and Scripting

Regex to identify word in second position on a line

I am interested in finding a regex to find a word in second position on a line. The word in question is या I tried the following PERL EXPRESSION but it did not work: ] या or ^\W या But both gave Null results I am giving below a Sample file: देना या सौंपना=delegate तह जमना या... (8 Replies)
Discussion started by: gimley
8 Replies

5. Shell Programming and Scripting

Regex to identify a full-stop as a sentence delimiter

Hello, Splitting a sentence using the full-stop/question-mark/exclamation is a common device. Whereas the question-mark / exclamation do not pose too much of a problem; the full-stop as a sentence delimiter raises certain issues because of its varied use: just to name a few. Standard parsers... (9 Replies)
Discussion started by: gimley
9 Replies

6. UNIX for Dummies Questions & Answers

Use Regex to identify / format a complex string

First of all, please have mercy on me. I am not a noob to programming, but I am about as noob as you can get with regex. That being said, I have a problem. I've got a string that looks something like this: Publication - Bob M. Jones, Tony X. Stark, and Fred D. Man, \"Really Awesome Article... (1 Reply)
Discussion started by: egill
1 Replies

7. UNIX and Linux Applications

Identify server.database connection

Good afternoon i need your help, i am new at unix, in a ETL scenario like datastage is , there are a bunch of procesess (script shells) conecting to hetereogenius database source servers in order to extract information. Ive got 2 questions 1. Using unix how can i identify exactly the... (1 Reply)
Discussion started by: alexcol
1 Replies

8. UNIX for Dummies Questions & Answers

Arabic characters in QNX4

I want to display Arabic characters in QNX4. This work was been done by a colleague several years ago but he didn't document his work. I installed fonts and I got this display (attached). Please let me know how can correct as per the initial display were working in Arabic (attached). Thanks... (0 Replies)
Discussion started by: hbc
0 Replies

9. Shell Programming and Scripting

how do I identify files with characters beyond a certain range.

I have a directory with hundreds of files that can not have data pass column 80. I do not know of way to combine "grep" and "cut" command. I tried: cat * | cut -c 81-120 |pg but it only shows me the line, not the file name. Any help would be appreciated. Been on this all... (3 Replies)
Discussion started by: kcsunsun01dev
3 Replies

10. UNIX for Dummies Questions & Answers

Illegal characters in Servername / Path

Hi there. I wonder if anybody can help me. I am very new to this and a bit out of my depth. I have a .cmd file which sets various environmental variables for me. When I input a server name that does not contains dots (.) in the name it works fine. As soon as I place in a server name... (5 Replies)
Discussion started by: goodjuju
5 Replies
Login or Register to Ask a Question