dogweather

dogweather

How to sanitize string input containing <<194, 160>> and maybe other junk?

I’m parsing a lot of Government web pages which were produced on ancient Microsoft systems. Like the .aspx Oregon Revised Statutes homepage. Or awkward not-really-html files like this “windows-1252” converted using MS Word and then put online. (!)

My parsing code is failing and I realized its because I have byte sequences like <<194, 160>> in the strings. (This might be Unicode nbsp?)

Here, that space between the 5 and the ( is actually <<194, 160>>:

iex(58)> :erlang.binary_to_list "1-55 (48)"
[49, 45, 53, 53, 194, 160, 40, 52, 56, 41]

One problem is, it doesn’t split or get caught by regex character classes like a space.

This has plagued me ever since I’ve worked with these docs - well over a decade :slight_smile: Here, I downloaded the file with curl, then read in with File.read!.

What’s a good way to go about working with these docs? What I want is to have just simple whitespace <<32>>, Not nbsp.

EDIT: I found that Regex will work with this data if I give it the :unicode (u) modifier.

pry(10)> raw_string
"Volume : 01 - Courts, Oregon Rules of Civil Procedure - Chapters 1-55 (48)"
pry(12)> Regex.run ~r/\w+-\w+/, raw_string
[<<49, 45, 53, 53, 194>>]
pry(12)> Regex.run ~r/\w+-\w+/u, raw_string
["1-55"]

I’m confused — I’m unsure whether raw_string is valid Unicode or not.

EDIT 2: I believe now that this is valid Unicode, which was correctly read in with File.read!. Unicode codepoint 160 is nbsp. But in UTF-8 it’s <<194, 160>> which is what I’m finding in my strings.

The puzzling part to me is that Regex requires the :unicode option to properly work with unicode. (Since we’re Unicode-first.) Another question I have is why doesn’t String.split/1 split on it?

Most Liked

kip

kip

ex_cldr Core Team

I think if José was starting again he might well default the regex to be unicode-compatible. But that would now be a breaking change, hence the issue that you see. Elixir wraps Erlang’s re module and uses the same defaults.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement