question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parsing a string with "boundaries"

See original GitHub issue

Hello.

I have had trouble coming up with a grammar for parsing a string that has a specific char at its “boundaries” in a greater path-like structure but which may also contain the boundary char internally.

So if the boundary char is * I need to pick out *foobar* in e.g. /foo/*foobar*/bar. My problem is that the *foobar* string may also contain the “boundary char” e.g. *foo*bar* so I cant use a negative char range for the parsing of the token between the /s.

Is it possible to do this with a PEG? And apologies if this is not the place for questions like this.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jonathanbpcommented, Nov 3, 2021

Great! Thanks a lot for your help, @Mingun and @hildjj - it is much appreciated.

1reaction
hildjjcommented, Nov 3, 2021

Taking mingun’s grammar as a start, I came up with this, which gets all of the edge cases I could think of:

path = "/"? head:segment tail:("/" @segment)* "/"? { return [head, ...tail] }

segment
  = special
  / normal
  
special = "*" text:$special_char+ "*" { return { type: "special", text } }
normal = text:$[^/]+ { return { type: "normal", text } }

special_char
  = [^*]      // Any non-* is valid
  / "*" &[^/] // Following a * with anything but a / makes it a part of the text
Read more comments on GitHub >

github_iconTop Results From Across the Web

Split on word boundaries. : String split « Data Type « Java
Use split() to extract substrings from a string. Split same string on commas and zero or more spaces. Split on word boundaries, but...
Read more >
Regex Boundaries and Delimiters—Standard and Advanced
Anchors assert that the current position in the string matches a certain position: the beginning, the end, or in the case of \G...
Read more >
Divide strings using String.Split (C# Guide) - Microsoft Learn
This method is often the easiest way to separate a string on word boundaries. It's also used to split strings on other specific...
Read more >
stri_split_boundaries: Split a String at Text Boundaries in stringi
This function locates text boundaries (like character, word, line, or sentence boundaries) and splits strings at the indicated positions. Usage.
Read more >
Splitting by word boundaries including apostrophes
I want to split each word of a string including spaces and punctuations in different groups, but I want to keep words with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found