MatthewMDavis
I’m trying all sorts of things with no luck, so I thought I’d ask for some help.
I want to split a string anytime there’s a “b” in “abc”, but not in " b ". I.e., when a “b” is surrounded by whitespace, I don’t want it to be a splitting character, but an element in the array returned by the split.
String.split("abcde", regex) == ["a", "cde"]
String.split("a b cde", regex) == ["a", "b", "cde"]
So I need (I think) a way to craft a regex that looks for abc, but only splits on the b part.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jwarlander
Regex Negative Lookbehind is your friend in cases like this:
The above pattern will match a
bfor splitting, only if it’s entirely surrounded by non-whitespace, eg. it’s not preceded by whitespace ((?<!\s)) and it’s not followed by whitespace ((?!\s)). It’ll also split on standalone whitespace ((...|\s)).Depending on your exact needs you may want to adjust the pattern, of course.
MatthewMDavis
Excellent. The resources I had looked at didn’t deal with lookbehind patterns.
JEG2
I think the pattern can be even simpler. Can you show some sample inputs and what you want to see as outputs?
MatthewMDavis
Well, I’m doing an exercism.io exercise that involves recreating a subset of Forth.
One of the test requirements is that non-word characters are separators between tokens, such that:
"1\x002\x013\n4\r5 6\t7" == "1 2 3 4 5 6 7"But the exercise involves building a toy language that can do subtraction, so I need a way to distinguish between hyphens used to separate word characters and hyphens used as subtraction symbols:
"1 2 + 4 -" == "1 2 + 4 -"So actually, what I need to represent in a regex is “all non-word characters EXCEPT a hyphen on its own.” This question was just to unstick me on that last part, finding the hyphen on its own.
JEG2
Could you just split out the terms and then process the resulting list?
MatthewMDavis
No, that won’t work. If I split on ALL non-word characters, my mathematical operators vanish. I need to be able to keep the operators (characters in the class [±*/], and surrounded by whitespace).
I may end up pre-processing the string and converting the operators to words for the operations they represent. Then if I just split on non-word characters, everything is easy. But I also have to re-convert them back to the original characters at the end. So this seemed like a potentially better way.
JEG2
Does this help?
MatthewMDavis
Oops. There’s supposed to be a hyphen between the 5 and the 6 of the input. I don’t know how that got left out
The trick is catching that hyphen when it’s between two word characters and splitting on it, but NOT splitting on a hyphen that is adrift in white space.
String.split(“1\x002\x013\n4\r5-6\t7”, regex) == “1 2 3 4 5 6 7”
String.split(“5 2 -”, regex) == [“5”, “2”, “-”]
JEG2
Can you give two or three string inputs and the lists you expect the code to produce from those strings? I think that would really help here.
swelham
I read this blog series(sorry it’s in python) last year that I think will help. It’s about building a simple interpreter and it starts off with simple maths and then onto supporting order of operation. Also requires no regex