Reputation: 3308
Let say I have below string -
Str = "15 1,270.00 19050 104.77 40.36 2020070 Rolling IN303028 - 63965276"
Now I want to split above string based on space
however it should not split last phrase i.e. 'IN303028 - 63965276'
. So I tried to use -
strsplit(Str, " ")
But this is also splitting the last phrase. Is there any way to splitting by space
but excluding the pattern of 'IN303028 - 63965276'
i.e. if two consecutive spaces
contains a hyphen
then it should exclude that part.
Any pointer will be highly appreciated.
Thanks,
Upvotes: 1
Views: 80
Reputation: 886948
We could make a regex lookaround to check for spaces after character that are not a -
([^-]
) and the characters that are after the spaces are not -
strsplit(Str, "(?<=[^-]) (?=[^-])", perl = TRUE)
#[[1]]
#[1] "15" "1,270.00" "19050" "104.77" "40.36" "2020070"
#[7] "Rolling" "IN303028 - 63965276"
Or use *SKIP
*FAIL
to not consider the spaces where there are -
strsplit(Str, " - (*SKIP)(*F)| ", perl = TRUE)
#[[1]]
#[1] "15" "1,270.00" "19050" "104.77" "40.36" "2020070"
#[7] "Rolling" "IN303028 - 63965276"
Upvotes: 4