Since FME uses a PERL implementation of RegEx, then could use StringSearcher with RegEx to find where there is a string that has a match to a non-ASCII character, and the character positions.RegEx Pattern looking for is[^[:ascii:]]Gives Answer from bwn on community.safe.com
Top answer
1 of 2
21

Regarding searching by UTF-16 code

To search by Unicode codepoints using UTF-16 you'd use \x{FEC1}, and it works whether the file is encoded with UTF-8 or UTF-16.

Bear in mind you wouldn't need to search by the UTF-8 code, because you can search by the UTF-16 code. But to address the part of your question that asks how do you search for that character by the UTF-8 code...

Regarding searching by UTF-8 code

You can't. Well, you sort of can, but it's a hideous hack and you really shouldn't.

The obvious thing to try would be to search for \xef\xbb\x81 in your UTF-8 encoded document, but that doesn't work. (Note there's no {} here: Notepad++ expects either \xNN for 2 hex digits, or \x{NNNN} for 4 hex digits). That's because Notepad++ doesn't actually search for byte values, it searches for Unicode codepoints. So you can search for the codepoint U+FEC1, but not for the UTF-8 bytes 0xEF 0xBB 0x81, because Notepad++ "hides" the encoding details from you. (Because in nearly every scenario, someone editing a text file will care far more about finding the actual character than about finding the UTF-8 bytes.)

There's another trick you might try, which is to take that UTF-8 encoded file and choose the Encoding → Encode in ANSI menu option, at which point ﻁﻁﻉﻁﻉﻁﻉ appears to become ï»ï»ï»‰ï»ï»‰ï»ï»‰. (I say "appears to become" rather than "becomes" because... well, read on.) This is because it has taken the UTF-8 text of your file, and reinterpreted it as "ANSI" (which is a terrible encoding name because it's completely wrong, and should really be called "Windows-1252", but that's a different question). (By the way, the reason that ﻁﻁﻉﻁﻉﻁﻉ looks backwards in my text than the way it does in your screenshot: that's because Notepad++ doesn't care that Arabic is written right-to-left, so it shows the characters left-to-right in the order they were pasted into the file. But your browser does care about presenting Arabic in proper right-to-left order, the first two letters of that string (ﻁﻁ) appear on the right-hand side of the string, not on the left-hand side as they seem to in Notepad++). Digressions aside, here's why this will be helpful. In the "ANSI" (really Windows-1252) encoding, each byte is a single character, and so now you're going to be able to search by individual bytes. Now, if you search for \xef\xbb\x81 (which doesn't need to be a regular expression, just an "Extended" search), it will find the characters. Sort of. It will look like it's highlighting the two characters ï», but it's really highlighting three characters: ï, », and an "invisible" 0x81 character that doesn't really exist. (Because there is no character at the 0x81 point in Windows-1252 encoding: see for yourself.) And now you see why I said "appears to become" -- because your UTF-8 encoded text has really become ï»_ï»_ﻉï»_ﻉï»_ﻉ, where _ represents an "invisible" character that doesn't officially exist in the Windows-1252 codepage. Anyway, now that you've found the sequence of three characters with the byte values 0xEF, 0xBB, and 0x81 in Windows-1252, and Notepad++ has highlighted them, you can choose the Encoding → Encode in UTF-8 menu option, and your text will convert itself back to UTF-8, while Notepad++ will keep the highlight in the same place -- and thus, you'll find that one character has been highlighted.

So why do I say that you really shouldn't do this? Because the only reason that it works is that Notepad++ didn't do the right thing when you switched codepages. The right thing to do when you find a missing character is to complain, or insert a character like the Unicode replacement character (or a simple ? if you're in a legacy codepage that doesn't have in it), or do something so that the user will know they had an invalid character in their text. Errors should never be silently ignored, and having a 0x81 value in Windows-1252 text is an error. The only reason this trick works is because Notepad++ does the wrong thing with invalid characters (that is, it ignores them). So you really shouldn't rely on this trick: with any update to Notepad++, it could change its undocumented (and wrong) behavior, and start putting proper replacement characters in wrongly-encoded text, at which point this trick would fail. Stick to searching for real Unicode codepoints, and you'll be much better off.

By the way, the reason why your original attempt ([\uFEC1]) failed is because, according to Notepad++'s regular expression syntax, \u means "an uppercase letter". (Remember that in regular expressions, brackets represent "any of these characters"). The docs further say, "See note about lower case [sic] letters," and the note about lowercase letters says "this will fall back on "a word character" if the "Match case" search option is off." As it is in your screenshot. Therefore, the regex [\uFEC1] is searching for "any word character, or F, or E, or C, or 1" -- which matches every single character in your sample text.

Phew, that turned out to be a very long answer for what I said would be "very simple". I hope this helps you understand Unicode a bit better; if so, the hour I spent typing this up will have been worth it.

2 of 2
9

Take a look: Anyone know how to use Regex in notepad++ to find Arabic characters?

Because Notepad++'s implementation of Regular Expressions requires that you use the

\x{NNNN}

notation to match Unicode characters.

In your example,

\x{FEC1} 
Discussions

Find non-ASCII characters in a text file and convert them to their Unicode equivalent - Stack Overflow
I am importing .txt file from a remote server and saving it to a database. I use a .Net script for this purpose. I sometimes notice a garbled word/characters (Ullerهkersvنgen) inside the files, which More on stackoverflow.com
🌐 stackoverflow.com
linux - How to grep for unicode � in a bash script - Unix & Linux Stack Exchange
That is a character used to replace any invalid Unicode character found in the text. It is a "visual aid", not a real character. To find and list every full line that contains invalid UNICODE characters use: ... If the result is 1 the file is clean, otherwise will be zero 0. More on unix.stackexchange.com
🌐 unix.stackexchange.com
regular expression - How to grep characters with their unicode value? - Unix & Linux Stack Exchange
I have the Unicode character ᚠ, represented by its Unicode code point 16A0, in a text file (the text file is encoded(?) as utf-8). More on unix.stackexchange.com
🌐 unix.stackexchange.com
June 6, 2019
Find Unicode Character in File
I have a very large file in Unix that I would like to search for all instances of the unicode character 0x17. I need to remove these characters because the character is causing my SAX Parser to throw an exception. Does anyone know how to find a unicode character in a file? More on unix.com
🌐 unix.com
1
0
October 19, 2017
Top answer
1 of 1
3

Emacs

C-x = (M-x what-cursor-position) shows code point information about the current character at the bottom of the screen, for example

Char: И (1048, #o2030, #x418, file ...) point=7 of 8 (75%) column=0

C-u C-x = opens a window with more information, including the code point, the byte representation, metadata about the Unicode character, what font is used to display the character, etc.

             position: 7 of 8 (75%), column: 0
            character: И (displayed as И) (codepoint 1048, #o2030, #x418)
    preferred charset: unicode (Unicode (ISO10646))
code point in charset: 0x0418
               script: cyrillic
               syntax: w    which means: word
             category: .:Base, L:Left-to-right (strong), Y:2-byte Cyrillic, c:Chinese, h:Korean, j:Japanese, y:Cyrillic
             to input: type "C-x 8 RET HEX-CODEPOINT" or "C-x 8 RET NAME"
          buffer code: #xD0 #x98
            file code: #xD0 #x98 (encoded by coding system utf-8-unix)
              display: by this font (glyph code)
    xft:-DAMA-Ubuntu Mono-normal-normal-normal-*-17-*-*-*-m-0-iso10646-1 (#x2CB)
         Unicode data:
                 Name: CYRILLIC CAPITAL LETTER I
             Category: Letter, Uppercase
      Combining class: Lu
        Bidi category: Lu
             Old name: CYRILLIC CAPITAL LETTER II
            Lowercase: и

Character code properties: customize what to show
  name: CYRILLIC CAPITAL LETTER I
  old-name: CYRILLIC CAPITAL LETTER II
  general-category: Lu (Letter, Uppercase)
  decomposition: (1048) ('И')

[back]

Command line

The unicode utility (available in some distributions such as Debian/Ubuntu/…, or with pip3 install unicode) shows information about one or more Unicode characters. Note that if you copy-paste from an editor, that editor may encode the clipboard differently from the file.

$ unicode И
U+0418 CYRILLIC CAPITAL LETTER I
UTF-8: d0 98 UTF-16BE: 0418 Decimal: И Octal: \02030
И (и)
Lowercase: 0438
Category: Lu (Letter, Uppercase)
Bidi: L (Left-to-Right)
Top answer
1 of 2
14

Assuming your script does know the correct encoding of your text snippet than that should be the regular expression to find all Non-ASCII charactres:

[^\x00-\x7F]+

see here: https://stackoverflow.com/a/20890052/1144966 and https://stackoverflow.com/a/8845398/1144966

Also, the base-R tools package provides two functions to detect non-ASCII characters:

tools::showNonASCII()
tools::showNonASCIIfile()
2 of 2
4

You need to know or at least guess the character encoding of the data in order to be able to convert it properly. So you should try and find information about the origin and format of the text file and make sure that you read the file properly in your software.

For example, “Ullerهkersvنgen” looks like a Scandinavian name, with Scandinavian letters in it, misinterpreted according to a wrong character encoding assumption or as munged by an incorrect character code conversion. The first Arabic letter in it, “ه”, is U+0647 ARABIC LETTER HEH. In the ISO-8859-6 encoding, it is E7 (hex.); in windows-1256, it is E5. Since Scandinavian text are normally represented in ISO-8859-1 or windows-1252 (when Unicode encodings are not used), it is natural to check what E7 and E5 mean in them: “ç” and “å”. For linguistic reasons, the latter is much more probable here. The second Arabic letter is “ن” U+0646 ARABIC LETTER NOON, which is E4 in windows-1256. And in ISO-8859-1, E4 is “ä”. This makes perfect sense: the word is “Ulleråkersvägen”, a real Swedish street name (in Uppsala, at least).

Thus, the data is probably ISO-8859-1 or windows-1252 (Windows Latin 1) encoded text, incorrectly interpreted as windows-1256 (Windows Arabic). No conversion is needed; you just need to read the data as windows-1252 encoded. (After reading, it can of course be converted to another encoding.)

🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 12964 › find-in-files-unicode-text
find in files unicode text | Notepad++ Community
December 29, 2016 - In Total Commander I can find text copy pasted from word documents when I tick the unicode box in its find text options. Sadly Total Commander doesn’t have find in files and replace like Notapad++ so…
🌐
Quora
quora.com › How-can-you-find-out-if-a-file-has-any-unicode-characters-in-it-in-Windows
How to find out if a file has any unicode characters in it in Windows - Quora
Answer: “How can you find out if a file has any unicode characters in it in Windows?” I am not sure what you mean, but I am pretty sure you are not asking the question you want to ask. Unicode is a superset of almost every encoding scheme that came before it (with a few exceptions, mostly ...
🌐
Soscisurvey
soscisurvey.de › tools › view-chars.php
View non-printable unicode characters
Online tool to display non-printable characters that may be hidden in copy&pasted strings.
Top answer
1 of 4
31

grep is the wrong tool for the job.

You see the � U+FFFD REPLACEMENT CHARACTER not because it’s literally in the file content, but because you looked at a binary file with a tool that is supposed to handle only text-based input. The standard way to handle invalid input (i.e., random binary data) is to replace everything that is not valid in the current locale (most probably UTF-8) with U+FFFD before it hits the screen.

That means it is very likely that a literal \xEF\xBF\xBD (the UTF-8 byte sequence for the U+FFFD character) never occurs in the file. grep is completely right in telling you, there is none.

One way to detect whether a file contains some unknown binary is with the file(1) command:

$ head -c 100 /dev/urandom > rubbish.bin
$ file rubbish.bin
rubbish.bin: data

For any unknown file type it will simply say data. Try

$ file out.txt | grep '^out.txt: data$'

to check whether the file really contains any arbitrary binary and thus most likely rubbish.

If you want to make sure that out.txt is a UTF-8 encoded text file only, you can alternatively use iconv:

$ iconv -f utf-8 -t utf-16 out.txt >/dev/null
2 of 4
16

TL;DR:

grep -axv '.*' out.txt 

long answer

Both present answers are extremely misleading and basically wrong.

To test, Get this two files (from a very well regarded developer: Markus Kuhn ):

$ wget https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt
$ wget https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt

Demo

The first UTF-8-demo.txt is a file designed to show how well UTF-8 is able to present many languages, math, braille and many other useful types of characters. Take a look with a text editor (that understand utf-8) and you will see a lot of examples and no .

The test that one answer propose: to limit the character range to \x00-\x7F will reject almost everything inside this file.
That is very wrong and will not remove any as there is none in that file.

Using the test recommended in that answer will remove 72.5 % of the file:

$ grep -oP "[^\x00-\x7F]" UTF-8-demo.txt | tr -d '\n' | wc -c
10192
$ cat UTF-8-demo.txt | wc -c
14058

That is (for most practical purposes) the whole file. A file very well designed to show perfectly valid characters.

Test

The second file is designed to try several border cases to confirm that utf-8 readers are doing a good job. It contains inside many characters that will cause a '�' to be shown. But the other answer recommendation (the selected one) to use file fails grossly with this file. Only removing a zero byte (\0) (which technically is valid ASCII) and a \x7f byte (DEL - delete) (which is clearly an ASCII character as well) will make all the file valid for the file command:

$ cat UTF-8-test.txt | tr -d '\0\177' > a.txt
$ file a.txt 
a.txt: Non-ISO extended-ASCII text, with LF, NEL line terminators

Not only does file fail to detect the many incorrect characters, but also fail to detect and report that it is an UTF-8 encoded file.

And yes, file is able to detect and report UTF-8 encoded text:

$ echo "ééakjfhhjhfakjfhfhaéá" | file -
/dev/stdin: UTF-8 Unicode text

Also, file fails to report as ASCII most of the control characters in the range 1 to 31. It (file) reports some ranges as data:

$ printf '%b' "$(printf '\\U%x' {1..6})" | file -
/dev/stdin: data

Others as ASCII text:

$ printf '%b' "$(printf '\\U%x' 7 {9..12})" | file -
/dev/stdin: ASCII text

As the printable character range (with newlines):

$ printf '%b' "$(printf '\\U%x' {32..126} 10)" | file -
/dev/stdin: ASCII text

But some ranges may cause weird results:

$ printf '%b' "$(printf '\\U%x' {14..26})" | file -
/dev/stdin: Atari MSA archive data, 4113 sectors per track, starting track: 5141, ending track: 5655

The program file is not a tool to detect text, but to detect magic numbers in executable programs or files.

The ranges file detect, and the corresponding type reported I found were:

  • One byte values, mostly ascii:

    {1..6} {14..26} {28..31} 127   :data
    {128..132} {134..159}          :Non-ISO extended-ASCII text
    133                            :ASCII text, with LF, NEL line terminators
    27                             :ASCII text, with escape sequences
    13                             :ASCII text, with CR, LF line terminators
    8                              :ASCII text, with overstriking
    7 {9..12} {32..126}            :ASCII text
    {160..255}                     :ISO-8859 text
    
  • Utf-8 encoded ranges:

    {1..6} {14..26} {28..31} 127   :data
    27                             :ASCII text, with escape sequences
    13                             :ASCII text, with CR, LF line terminators
    8                              :ASCII text, with overstriking
    7 {9..12} {32..126}            :ASCII text
    {128..132} {134..159}          :UTF-8 Unicode text
    133                            :UTF-8 Unicode text, with LF, NEL line terminators
    {160..255}                     :UTF-8 Unicode text
    {256..5120}                    :UTF-8 Unicode text
    

One possible solution lies below.


Previous Answer.

The Unicode value for the character you are posting is:

$ printf '%x\n' "'�"
fffd

Yes, that is a Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD). That is a character used to replace any invalid Unicode character found in the text. It is a "visual aid", not a real character. To find and list every full line that contains invalid UNICODE characters use:

grep -axv '.*' out.txt 

but if you only want to detect if any character is invalid, use:

grep -qaxv '.*' out.txt; echo $?

If the result is 1 the file is clean, otherwise will be zero 0.


If what you were asking was: how to find the character, then, use this:

➤ a='Basically, if the file "out.txt" contains "�" anywhere in the file I'
➤ echo "$a" | grep -oP $(printf %b \\Ufffd)
�

Or if your system process correctly UTF-8 text, simply:

➤ echo "$a" | grep -oP '�'
�
Find elsewhere
🌐
Unicode
unicode.org › standard › where
Where is my Character?
If you are trying to find a specific character in the Unicode Standard, the first place to go is the code charts. The code charts are organized into blocks, which are groupings of related characters · For each character defined in Unicode you will find an assigned code point: a hexadecimal ...
🌐
Unix.com
unix.com › shell programming and scripting
Find Unicode Character in File - Shell Programming and Scripting - Unix Linux Community
October 19, 2017 - I have a very large file in Unix that I would like to search for all instances of the unicode character 0x17. I need to remove these characters because the character is causing my SAX Parser to throw an exception. Does…
🌐
Reddit
reddit.com › r/c_programming › read unicode content from a file
r/C_Programming on Reddit: Read unicode content from a file
December 23, 2022 -

There is a text file. And the content of the file is "❄⛵🎶" characters. How can i get the content of the file character by character. I need to get character or its hex value into a variable. How can i do this without using wide charcters or external libraries?

🌐
Total Commander
ghisler.ch › home › index › total commander › total commander (english)
How to find detect Unicode UTF-16 UTF-8 txt files in search? - Total Commander
December 18, 2014 - There is no completely reliable way to detect Unicode (UTF-16) files, since it relies on some statistics, but it works in most cases. For UTF-8 it is more reliable, it might only fail for some (very) short text snippets. The only plugins I'm aware of: EncInfo It fails sometimes, since it tries ...
🌐
SAP Blogs
blogs.sap.com › 2016 › 09 › 06 › test-9
How to identify Non-unicode characters in a Text file
September 6, 2016 - Mozilla Firefox will show us the row and column number along with the content of that row and column.An underscore will be till the column where the non-unicode character is lying.If there are multiple non Unicode characters in the .txt file then we should remove the first non-unicode character that was identified and then repeat all the steps as explained here to identify the next non-unicode character.Tedious,but this way atleast we can identify the presence of non-unicode characters in the text file.
🌐
Google Groups
groups.google.com › g › bbedit › c › gh7oEmJ8YHg
Search for unicode character
In my memory far away, I thought in BBEdit it is possible to search directly for a Unicode, something like: \u{0x0308} ??? I discovered in my text filters an old filter, which I put up long time ago and I called it "pdf_to_text.pl" If you are running it over the text, which you copy and pasted from a pdf to your txt file, it replaces the "wrong" Umlauts with the right ones. In this script all special characters out of the range of ASCII are replaced with the real ones (I can't explain it better).
🌐
Sublime Forum
forum.sublimetext.com › t › searching-for-unicode-char › 36184
Searching for Unicode char - Technical Support - Sublime Forum
April 13, 2018 - I have yet to find an editor that can find Unicode character U+ED06. New to Sublime and it’s essential to locate offending char that don’t process. Any help would be greatly appreciated. Thanks, Frank
🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 10659 › search-for-unicode-char
Search for unicode char? | Notepad++ Community
October 12, 2015 - By default, you must type, on top of the page, the UNICODE hexadecimal code-point of your character ( For instance fec9 ), but you may select one of the six other proposed interpretations : ... Hope that this post will be useful to you, anyway !