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.

Question/Bug: how do I preserve dashes and not replace them with separator?

See original GitHub issue

I am trying to slugify a string, replacing non-matching characters with an underscore, but I don’t want to consider a dash a non-matching character. This is easily achieved with regex but it looks like there is some special slugify behaviour when it comes to dashes which is preventing this working. I can’t see anything in the docs or code, about how to work around this.

What did I do?

slugify.slugify("test-foo bar", separator='_', regex_pattern=r'[^-a-zA-Z0-9_]+')

What happened? test_foo_bar

What did I expect? test-foo_bar

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
adamcunnington-mlgcommented, Dec 14, 2021

@PradeepTammali workaround is something like this:

s = str.replace('-', 'thisSucks')
slugify(s, separator='_', regex_pattern='[^a-zA-Z0-9_]+').replace('thisSucks', '-')
0reactions
un33kcommented, Dec 16, 2021

@PradeepTammali Slugify needs one reserved character, and since the name of the package is slugify its job is to convert some random text to a dash-delimited clean text for use in URLs. With that said, the delimiter can also be changed to whatever user wants. In short, the ask here is not doable. I encourage channelling one’s energy to raising a meaningful PR instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replace spaces with dashes and make all letters lower-case
I want to convert it to "sonic-free-games" . So whitespaces should be replaced by dashes and all letters converted to small letters. Any...
Read more >
awk - Bash - remove dashes and new-lines before replacing ...
Remove dashes used for hyphenation at the end of lines (joining hyphenated lines with the next line in the process) ...
Read more >
The Hyphen and the Dash - University of Sussex
That's all there is to know about the dash. Use the dash carefully: overuse of dashes will give your writing a breathless and...
Read more >
Convert Text to Table: Cannot Enter a Dash in Options Box
I figured out how to find & replace the dashes with tabs. So, I replace the ( - ) instances with tabs. I...
Read more >
Program to replace every space in a string with hyphen
If the character is a space, replace it with the hyphen '-'. Below is the implementation of the above approach:.
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