Giving the following string...
"Here is my very _special string_ with {different} types of _delimiters_ that might even {repeat a few times}."
... how to split it into an array using the 2 delimiters ("_", "{ and }") but also keeping the delimiters in each element of the array?
The goal is:
[
"Here is my very ",
"_special string_",
" with ",
"{different}",
" types of ",
"_delimiters_",
"that might even ",
"{repeat a few times}",
"."
]
My best bet was:
let myText = "Here is my very _special string_ with {different} types of _delimiters_ that might even {repeat a few times}."
console.log(myText.split(/(?=_|{|})/g))
as you see, it fails to reproduce the desirable array.
To split a string with multiple delimiters in Python, use the re. split() method. The re. split() function splits the string by each occurrence of the pattern.
Python has a built-in method you can apply to string, called . split() , which allows you to split a string by a certain delimiter.
You may use
s.split(/(_[^_]*_|{[^{}]*})/).filter(Boolean)
See the regex demo. The whole pattern is enclosed in a capturing group, so all matching substrings are included in the resulting array after String#split
.
Regex details
(_[^_]*_|{[^{}]*})
- Capturing group 1:
_[^_]*_
- _
, 0 or more chars other than _
and then a _
|
- or{[^{}]*}
- a {
, then any 0 or more chars other than {
and }
and then a }
See JS demo:
var s = "Here is my very _special string_ with {different} types of _delimiters_ that might even {repeat a few times}.";
console.log(s.split(/(_[^_]*_|{[^{}]*})/).filter(Boolean));
You can use the following regex which returns some undefined
values, finally, you can filter the undefined
values.
let myText = "Here is my very _special string_ with {different} types of _delimiters_ that might even {repeat a few times}."
console.log(myText.split(/(_.+?_)|({.+?})/g).filter(Boolean))
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With