So I have a string that is made up of multiple items and is split with either a "+" or a "-". The string contains 5 elements: typeCode, versionCode, lengthCode, partCode, and the last part is data. The length of each type would need to be variable, so typeCode could have a length of 2 but it could also have a length of 5. The max length of the total string is 30 characters but it may be shorter depending on the length of the data at the end. I'm able to split it when the length of each part is fixed (2 is what I've worked with) but I want it to be flexible and not hardcoded.
I'd like to split each string into its 5 elements as an array. The separator of each element is either a "+" or a "-" as shown in the examples. I do not want the separator included in any of the elements. There is no splitting char preceding the first element or after the last element.
I'd also like to be able to determine whether the last seperator is a "+" or a "-" and have that as an element in the array.
I believe that this may be done with RegEx using String.split(separator) where separator is a string or RegEx in javascript but I'm not sure. I don't know RegEx.
Examples of input and desired output:
01+03+03+00-3f2aec1f6b088a1c
= ["01","03","03","00", "-", "3f2aec1f6b088a1c"]
01+03+03+01+acaa4ece96da0e31
= ["01", "03", "03", "01", "+", "acaa4ece96da0e31"'
011+031+03+01+acaa4ece96da0e31
= ["011", "031", "03", "01", "+", "acaa4ece96da0e31"
So I have a string that is made up of multiple items and is split with either a "+" or a "-". The string contains 5 elements: typeCode, versionCode, lengthCode, partCode, and the last part is data. The length of each type would need to be variable, so typeCode could have a length of 2 but it could also have a length of 5. The max length of the total string is 30 characters but it may be shorter depending on the length of the data at the end. I'm able to split it when the length of each part is fixed (2 is what I've worked with) but I want it to be flexible and not hardcoded.
I'd like to split each string into its 5 elements as an array. The separator of each element is either a "+" or a "-" as shown in the examples. I do not want the separator included in any of the elements. There is no splitting char preceding the first element or after the last element.
I'd also like to be able to determine whether the last seperator is a "+" or a "-" and have that as an element in the array.
I believe that this may be done with RegEx using String.split(separator) where separator is a string or RegEx in javascript but I'm not sure. I don't know RegEx.
Examples of input and desired output:
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 18, 2018 at 19:06 smwsmw 551 silver badge7 bronze badges 401+03+03+00-3f2aec1f6b088a1c
= ["01","03","03","00", "-", "3f2aec1f6b088a1c"]
01+03+03+01+acaa4ece96da0e31
= ["01", "03", "03", "01", "+", "acaa4ece96da0e31"'
011+031+03+01+acaa4ece96da0e31
= ["011", "031", "03", "01", "+", "acaa4ece96da0e31"
- 3 ok and what have you tried so far? – messerbill Commented Jan 18, 2018 at 19:08
-
1
Regex for plus or minus would be
[+-]
. – PM 77-1 Commented Jan 18, 2018 at 19:09 - Please show where you are in your code and someone may be able to help point you in the right direction. StackOverflow isn't the sort of place where you can ask questions and just get programmers to write your code for you. – Tad Donaghe Commented Jan 18, 2018 at 19:09
- I was just needing the Regex expression to use for the String.split() function. rollstuhlfahrer has provided me with the expression that splits the string in the answers. I don't know regex, I tried (+|-) and other random stuff but I wasn't really sure what I was doing. What I have now just hardcodes the splitting but it doesn't work when the elements are different sizes. – smw Commented Jan 18, 2018 at 19:17
2 Answers
Reset to default 6/[+-]/g
does exactly what you want.
From the Javascript console:
"01+03+03+00-3f2aec1f6b088a1c".split(/[+-]/g)
> (5) ["01", "03", "03", "00", "3f2aec1f6b088a1c"]
To find the last delimiter, you can pick it from the input string:
data = "01+03+03+00-3f2aec1f6b088a1c";
chunks = data.split(/[+-]/g);
lastDelimiter = data.charAt(data.length - chunks[chunks.length - 1].length - 1)
Or you can use the regex, @Jeffrey Westerkamp suggested in the ments
(-|\+)[^+-]+$
Other possibility to catch the last delimiter:
"01+03+03+00-3f2aec1f6b088a1c".split(/[+-](?!\w+$)|([+-])/).filter(i=>i)
or to be more rigorous:
"01+03+03+00-3f2aec1f6b088a1c".split(/[+-](?!\w+$)|([+-])/).filter(i=>i!==undefined)
The idea consists to use the first branch for all delimiters that are not the last delimiter (tested with the negative lookahead (?!\w+$)
) and to use the second branch with a capture group for the last delimiter. Since the capture group isn't defined for all delimiters that aren't the last, you need to filter all the undefined items in the result array.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744160803a4561080.html
评论列表(0条)