Input should be like this:
class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath
Output something like:
["class1{name='adam.smith'}", "class2{name='john'}", "subjectMath"]
Any solution?
Input should be like this:
class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath
Output something like:
["class1{name='adam.smith'}", "class2{name='john'}", "subjectMath"]
Any solution?
Try using split
:
var input = "class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath";
var parts = input.split(/\.(?![^{]*’)/);
console.log(parts);
The regex used for the split requires some explanation:
\. match a literal dot
(?![^{]*’) but assert that we DON'T encounter a text curly quote
looking forward so long as we don't hit a { first
The negative lookahead fails the dot in adam.smith
, because we can find a curly quote without encountering a {
opening bracket, which would imply that the dot is not a connecting dot.
Giving a sting like this:
string = "class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath";
You could try with this:
string.split(/(?<=})./)
which will return:
[ "class1{name=‘adam.smith’}" , "class2{name=‘john’}" , "subjectMath" ]
This is a solution for Swift assuming that the input structure remains the same, with the bracket preceding the dot :
var input = "class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath"
func splittingClasses(_ input: String) -> [String] {
var total: [String] = []
var parts = input.components(separatedBy: "}.")
let endpart = "}"
for i in 0 ..< parts.count {
if i == parts.count - 1 {
total.append(parts[i])
} else {
total.append(parts[i] + endpart)
}
}
print(total)
return total
}
splittingClasses(input)
// returns ["class1{name=‘adam.smith’}", "class2{name=‘john’}", "subjectMath"]
You could match everything that is not an .
of course this gives some issues with the .
within ‘...’
context. For this reason we should also match those.
const input = "class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath";
const output = input.match(/(?:‘[^’]*’|[^.])+/g);
console.log(output);
‘[^’]*’
Will match any ‘
and keeps matching until it finds the closing ’
character, matching anything in-between.
[^.]
Will match anything that is not a .
character.