Split phone numbers into groups based on first digitInserting an A1 image into an A4 document whilst maintaining page numbers?How to add spaces at every X digit in a binary or hexadecimal number?Header, dots in split equation and parentheses around footnote numbersSplit listing into multiple sectionsTransforming numbers in a matrix structure into color squares
In which case does the Security misconfiguration vulnerability apply to?
A torrent of foreign terms
80's/90's superhero cartoon with a man on fire and a man who made ice runways like Frozone
Why aren’t there water shutoff valves for each room?
Should I email my professor about a recommendation letter if he has offered me a job?
Can renaming a method preserve encapsulation?
Can sampling rate be a floating point number?
Why is there a large performance impact when looping over an array over 240 elements?
Boss asked a co-worker to assault me
Generate Brainfuck for the numbers 1–255
How exactly are corporate bonds priced at issue
What to do with a williow tree with a cracked split trunk due to strong winds
Why aren't rockets built with truss structures inside their fuel & oxidizer tanks to increase structural strength?
Word for an event that will likely never happen again
What is the difference between 王 and 皇?
PhD advisor lost funding, need advice
Is this n-speak?
Do I have to cite common CS algorithms?
Can lodestones be used to magnetize crude iron weapons?
Is there any way to stop a user from creating executables and running them?
Is it okay for a ticket seller to grab a tip in the USA?
How to Check all AD userers for "blank" password?
How do I call a 6 digit Austrailian phone number with a US based mobile phone?
Is there a way to count the number of lines of text in a file including non-delimited ones?
Split phone numbers into groups based on first digit
Inserting an A1 image into an A4 document whilst maintaining page numbers?How to add spaces at every X digit in a binary or hexadecimal number?Header, dots in split equation and parentheses around footnote numbersSplit listing into multiple sectionsTransforming numbers in a matrix structure into color squares
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Problem
I am trying to split some numbers based on some very simple criteria. Numbers with 8 digits starting with 4, 8 or 9 should be split into digit groups of length 3 2 3.
Any other digit of length 8 should be split into groups of two.
Numbers with less digits than 8 should not be split into groups.
Examples
- 23 27 60 11 (hard spaces)
- 404 43 033 (hard spaces, 3 2 3)
- 820 43 033 (hard spaces, 3 2 3)
- 909 64 159 (hard spaces)
- 07979 (no spaces)
- 110 (no spaces), 112 (no spaces), 113 (no spaces)
Sorry for the lack of MWE, but I am not quite sure where to start.
formatting number
add a comment |
Problem
I am trying to split some numbers based on some very simple criteria. Numbers with 8 digits starting with 4, 8 or 9 should be split into digit groups of length 3 2 3.
Any other digit of length 8 should be split into groups of two.
Numbers with less digits than 8 should not be split into groups.
Examples
- 23 27 60 11 (hard spaces)
- 404 43 033 (hard spaces, 3 2 3)
- 820 43 033 (hard spaces, 3 2 3)
- 909 64 159 (hard spaces)
- 07979 (no spaces)
- 110 (no spaces), 112 (no spaces), 113 (no spaces)
Sorry for the lack of MWE, but I am not quite sure where to start.
formatting number
What about numbers with more than 8 digits?
– Marijn
11 hours ago
add a comment |
Problem
I am trying to split some numbers based on some very simple criteria. Numbers with 8 digits starting with 4, 8 or 9 should be split into digit groups of length 3 2 3.
Any other digit of length 8 should be split into groups of two.
Numbers with less digits than 8 should not be split into groups.
Examples
- 23 27 60 11 (hard spaces)
- 404 43 033 (hard spaces, 3 2 3)
- 820 43 033 (hard spaces, 3 2 3)
- 909 64 159 (hard spaces)
- 07979 (no spaces)
- 110 (no spaces), 112 (no spaces), 113 (no spaces)
Sorry for the lack of MWE, but I am not quite sure where to start.
formatting number
Problem
I am trying to split some numbers based on some very simple criteria. Numbers with 8 digits starting with 4, 8 or 9 should be split into digit groups of length 3 2 3.
Any other digit of length 8 should be split into groups of two.
Numbers with less digits than 8 should not be split into groups.
Examples
- 23 27 60 11 (hard spaces)
- 404 43 033 (hard spaces, 3 2 3)
- 820 43 033 (hard spaces, 3 2 3)
- 909 64 159 (hard spaces)
- 07979 (no spaces)
- 110 (no spaces), 112 (no spaces), 113 (no spaces)
Sorry for the lack of MWE, but I am not quite sure where to start.
formatting number
formatting number
asked 11 hours ago
N3buchadnezzarN3buchadnezzar
4,7655 gold badges42 silver badges98 bronze badges
4,7655 gold badges42 silver badges98 bronze badges
What about numbers with more than 8 digits?
– Marijn
11 hours ago
add a comment |
What about numbers with more than 8 digits?
– Marijn
11 hours ago
What about numbers with more than 8 digits?
– Marijn
11 hours ago
What about numbers with more than 8 digits?
– Marijn
11 hours ago
add a comment |
2 Answers
2
active
oldest
votes
This can be done with the xstring package, which provides commands for extracting and comparing substrings.
It is possible to do some preprocessing to the string before the formatting is applied, for example to remove whitespace. The xstring commands for extracting substrings, modifications and counts have the general syntax Commandarg1...[result] where the optional result argument stores the result for further processing (if this argument is not given the result is just printed directly). This can be used for preprocessing to store the preprocessed string and do the rest of the processing on the result string.
MWE:
documentclassarticle
newififstartnum
usepackagexstring
newcommandsplitdigits[1]%
StrDel#1 [newstring]%
StrLennewstring[mylen]%
ifnum mylen=8 %
startnumfalse%
IfBeginWithnewstring4startnumtrue%
IfBeginWithnewstring8startnumtrue%
IfBeginWithnewstring9startnumtrue%
ifstartnum%
StrLeftnewstring3 StrMidnewstring45 StrRightnewstring3%
else%
StrLeftnewstring2 StrMidnewstring34 StrMidnewstring56 StrRightnewstring2%
fi%
else%
#1%
fi%
begindocument
noindentsplitdigits23276011\
splitdigits40443033\
splitdigits82043033\
splitdigits90964159\
splitdigits07979\
splitdigits110 splitdigits112 splitdigits113\
splitdigits9 09 6415 9\
splitdigits90 96 41 59\
splitdigits232 76 011
enddocument
Result:

Great answer! Is it possible to also handle inputs such atsplitdigits9 09 6415 9andsplitdigits90 96 41 59,splitdigits232 76 011?
– N3buchadnezzar
7 hours ago
@N3buchadnezzar that is possible, you can delete all spaces before you format the numbers usingStrDel. See page 8 of the manual.
– Marijn
7 hours ago
@Marjin Thanks! But I still can not make it work. WritingdefnewnumStrDel400 7519 newnum splitdigitsnewnumDoes not work as it expands too late.
– N3buchadnezzar
6 hours ago
@N3buchadnezzar the idea is to do the preprocessing inside of the command, see edit.
– Marijn
6 hours ago
Thanks a lot, I tried to do as you did but withnewcommandnewstring[1]StrDel#1is the problem with this approach that the result expands too late?
– N3buchadnezzar
6 hours ago
|
show 1 more comment
Branch according to the length of the argument: if it is eight digit long, branch with respect to the first digit; otherwise print the argument.
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandphonem
nebu_phone:n #1
cs_new:Nn nebu_phone:n
int_compare:nTF tl_count:n #1 = 8
__nebu_phone_eight:n #1
#1
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone23276011
phone40443033
phone82043033
phone90964159
phone07979
phone110, phone112, phone113
enddocument

If you want to remove spaces from the input, you have to give up expandability:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandphonem
nebu_phone:n #1
tl_new:N l__nebu_phone_tl
cs_new_protected:Nn nebu_phone:n
tl_set:Nn l__nebu_phone_tl #1
tl_remove_all:Nn l__nebu_phone_tl ~
int_compare:nTF tl_count:N l__nebu_phone_tl = 8
__nebu_phone_eight:V l__nebu_phone_tl
tl_use:N l__nebu_phone_tl
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_generate_variant:Nn __nebu_phone_eight:n V
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone232 76 011
phone40 44 30 33
phone820 430 33
phone90964159
phone079 79
phone110, phone112, phone113
enddocument
The output is the same.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f504040%2fsplit-phone-numbers-into-groups-based-on-first-digit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can be done with the xstring package, which provides commands for extracting and comparing substrings.
It is possible to do some preprocessing to the string before the formatting is applied, for example to remove whitespace. The xstring commands for extracting substrings, modifications and counts have the general syntax Commandarg1...[result] where the optional result argument stores the result for further processing (if this argument is not given the result is just printed directly). This can be used for preprocessing to store the preprocessed string and do the rest of the processing on the result string.
MWE:
documentclassarticle
newififstartnum
usepackagexstring
newcommandsplitdigits[1]%
StrDel#1 [newstring]%
StrLennewstring[mylen]%
ifnum mylen=8 %
startnumfalse%
IfBeginWithnewstring4startnumtrue%
IfBeginWithnewstring8startnumtrue%
IfBeginWithnewstring9startnumtrue%
ifstartnum%
StrLeftnewstring3 StrMidnewstring45 StrRightnewstring3%
else%
StrLeftnewstring2 StrMidnewstring34 StrMidnewstring56 StrRightnewstring2%
fi%
else%
#1%
fi%
begindocument
noindentsplitdigits23276011\
splitdigits40443033\
splitdigits82043033\
splitdigits90964159\
splitdigits07979\
splitdigits110 splitdigits112 splitdigits113\
splitdigits9 09 6415 9\
splitdigits90 96 41 59\
splitdigits232 76 011
enddocument
Result:

Great answer! Is it possible to also handle inputs such atsplitdigits9 09 6415 9andsplitdigits90 96 41 59,splitdigits232 76 011?
– N3buchadnezzar
7 hours ago
@N3buchadnezzar that is possible, you can delete all spaces before you format the numbers usingStrDel. See page 8 of the manual.
– Marijn
7 hours ago
@Marjin Thanks! But I still can not make it work. WritingdefnewnumStrDel400 7519 newnum splitdigitsnewnumDoes not work as it expands too late.
– N3buchadnezzar
6 hours ago
@N3buchadnezzar the idea is to do the preprocessing inside of the command, see edit.
– Marijn
6 hours ago
Thanks a lot, I tried to do as you did but withnewcommandnewstring[1]StrDel#1is the problem with this approach that the result expands too late?
– N3buchadnezzar
6 hours ago
|
show 1 more comment
This can be done with the xstring package, which provides commands for extracting and comparing substrings.
It is possible to do some preprocessing to the string before the formatting is applied, for example to remove whitespace. The xstring commands for extracting substrings, modifications and counts have the general syntax Commandarg1...[result] where the optional result argument stores the result for further processing (if this argument is not given the result is just printed directly). This can be used for preprocessing to store the preprocessed string and do the rest of the processing on the result string.
MWE:
documentclassarticle
newififstartnum
usepackagexstring
newcommandsplitdigits[1]%
StrDel#1 [newstring]%
StrLennewstring[mylen]%
ifnum mylen=8 %
startnumfalse%
IfBeginWithnewstring4startnumtrue%
IfBeginWithnewstring8startnumtrue%
IfBeginWithnewstring9startnumtrue%
ifstartnum%
StrLeftnewstring3 StrMidnewstring45 StrRightnewstring3%
else%
StrLeftnewstring2 StrMidnewstring34 StrMidnewstring56 StrRightnewstring2%
fi%
else%
#1%
fi%
begindocument
noindentsplitdigits23276011\
splitdigits40443033\
splitdigits82043033\
splitdigits90964159\
splitdigits07979\
splitdigits110 splitdigits112 splitdigits113\
splitdigits9 09 6415 9\
splitdigits90 96 41 59\
splitdigits232 76 011
enddocument
Result:

Great answer! Is it possible to also handle inputs such atsplitdigits9 09 6415 9andsplitdigits90 96 41 59,splitdigits232 76 011?
– N3buchadnezzar
7 hours ago
@N3buchadnezzar that is possible, you can delete all spaces before you format the numbers usingStrDel. See page 8 of the manual.
– Marijn
7 hours ago
@Marjin Thanks! But I still can not make it work. WritingdefnewnumStrDel400 7519 newnum splitdigitsnewnumDoes not work as it expands too late.
– N3buchadnezzar
6 hours ago
@N3buchadnezzar the idea is to do the preprocessing inside of the command, see edit.
– Marijn
6 hours ago
Thanks a lot, I tried to do as you did but withnewcommandnewstring[1]StrDel#1is the problem with this approach that the result expands too late?
– N3buchadnezzar
6 hours ago
|
show 1 more comment
This can be done with the xstring package, which provides commands for extracting and comparing substrings.
It is possible to do some preprocessing to the string before the formatting is applied, for example to remove whitespace. The xstring commands for extracting substrings, modifications and counts have the general syntax Commandarg1...[result] where the optional result argument stores the result for further processing (if this argument is not given the result is just printed directly). This can be used for preprocessing to store the preprocessed string and do the rest of the processing on the result string.
MWE:
documentclassarticle
newififstartnum
usepackagexstring
newcommandsplitdigits[1]%
StrDel#1 [newstring]%
StrLennewstring[mylen]%
ifnum mylen=8 %
startnumfalse%
IfBeginWithnewstring4startnumtrue%
IfBeginWithnewstring8startnumtrue%
IfBeginWithnewstring9startnumtrue%
ifstartnum%
StrLeftnewstring3 StrMidnewstring45 StrRightnewstring3%
else%
StrLeftnewstring2 StrMidnewstring34 StrMidnewstring56 StrRightnewstring2%
fi%
else%
#1%
fi%
begindocument
noindentsplitdigits23276011\
splitdigits40443033\
splitdigits82043033\
splitdigits90964159\
splitdigits07979\
splitdigits110 splitdigits112 splitdigits113\
splitdigits9 09 6415 9\
splitdigits90 96 41 59\
splitdigits232 76 011
enddocument
Result:

This can be done with the xstring package, which provides commands for extracting and comparing substrings.
It is possible to do some preprocessing to the string before the formatting is applied, for example to remove whitespace. The xstring commands for extracting substrings, modifications and counts have the general syntax Commandarg1...[result] where the optional result argument stores the result for further processing (if this argument is not given the result is just printed directly). This can be used for preprocessing to store the preprocessed string and do the rest of the processing on the result string.
MWE:
documentclassarticle
newififstartnum
usepackagexstring
newcommandsplitdigits[1]%
StrDel#1 [newstring]%
StrLennewstring[mylen]%
ifnum mylen=8 %
startnumfalse%
IfBeginWithnewstring4startnumtrue%
IfBeginWithnewstring8startnumtrue%
IfBeginWithnewstring9startnumtrue%
ifstartnum%
StrLeftnewstring3 StrMidnewstring45 StrRightnewstring3%
else%
StrLeftnewstring2 StrMidnewstring34 StrMidnewstring56 StrRightnewstring2%
fi%
else%
#1%
fi%
begindocument
noindentsplitdigits23276011\
splitdigits40443033\
splitdigits82043033\
splitdigits90964159\
splitdigits07979\
splitdigits110 splitdigits112 splitdigits113\
splitdigits9 09 6415 9\
splitdigits90 96 41 59\
splitdigits232 76 011
enddocument
Result:

edited 6 hours ago
answered 10 hours ago
MarijnMarijn
11.4k1 gold badge6 silver badges40 bronze badges
11.4k1 gold badge6 silver badges40 bronze badges
Great answer! Is it possible to also handle inputs such atsplitdigits9 09 6415 9andsplitdigits90 96 41 59,splitdigits232 76 011?
– N3buchadnezzar
7 hours ago
@N3buchadnezzar that is possible, you can delete all spaces before you format the numbers usingStrDel. See page 8 of the manual.
– Marijn
7 hours ago
@Marjin Thanks! But I still can not make it work. WritingdefnewnumStrDel400 7519 newnum splitdigitsnewnumDoes not work as it expands too late.
– N3buchadnezzar
6 hours ago
@N3buchadnezzar the idea is to do the preprocessing inside of the command, see edit.
– Marijn
6 hours ago
Thanks a lot, I tried to do as you did but withnewcommandnewstring[1]StrDel#1is the problem with this approach that the result expands too late?
– N3buchadnezzar
6 hours ago
|
show 1 more comment
Great answer! Is it possible to also handle inputs such atsplitdigits9 09 6415 9andsplitdigits90 96 41 59,splitdigits232 76 011?
– N3buchadnezzar
7 hours ago
@N3buchadnezzar that is possible, you can delete all spaces before you format the numbers usingStrDel. See page 8 of the manual.
– Marijn
7 hours ago
@Marjin Thanks! But I still can not make it work. WritingdefnewnumStrDel400 7519 newnum splitdigitsnewnumDoes not work as it expands too late.
– N3buchadnezzar
6 hours ago
@N3buchadnezzar the idea is to do the preprocessing inside of the command, see edit.
– Marijn
6 hours ago
Thanks a lot, I tried to do as you did but withnewcommandnewstring[1]StrDel#1is the problem with this approach that the result expands too late?
– N3buchadnezzar
6 hours ago
Great answer! Is it possible to also handle inputs such at
splitdigits9 09 6415 9 and splitdigits90 96 41 59, splitdigits232 76 011?– N3buchadnezzar
7 hours ago
Great answer! Is it possible to also handle inputs such at
splitdigits9 09 6415 9 and splitdigits90 96 41 59, splitdigits232 76 011?– N3buchadnezzar
7 hours ago
@N3buchadnezzar that is possible, you can delete all spaces before you format the numbers using
StrDel. See page 8 of the manual.– Marijn
7 hours ago
@N3buchadnezzar that is possible, you can delete all spaces before you format the numbers using
StrDel. See page 8 of the manual.– Marijn
7 hours ago
@Marjin Thanks! But I still can not make it work. Writing
defnewnumStrDel400 7519 newnum splitdigitsnewnum Does not work as it expands too late.– N3buchadnezzar
6 hours ago
@Marjin Thanks! But I still can not make it work. Writing
defnewnumStrDel400 7519 newnum splitdigitsnewnum Does not work as it expands too late.– N3buchadnezzar
6 hours ago
@N3buchadnezzar the idea is to do the preprocessing inside of the command, see edit.
– Marijn
6 hours ago
@N3buchadnezzar the idea is to do the preprocessing inside of the command, see edit.
– Marijn
6 hours ago
Thanks a lot, I tried to do as you did but with
newcommandnewstring[1]StrDel#1 is the problem with this approach that the result expands too late?– N3buchadnezzar
6 hours ago
Thanks a lot, I tried to do as you did but with
newcommandnewstring[1]StrDel#1 is the problem with this approach that the result expands too late?– N3buchadnezzar
6 hours ago
|
show 1 more comment
Branch according to the length of the argument: if it is eight digit long, branch with respect to the first digit; otherwise print the argument.
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandphonem
nebu_phone:n #1
cs_new:Nn nebu_phone:n
int_compare:nTF tl_count:n #1 = 8
__nebu_phone_eight:n #1
#1
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone23276011
phone40443033
phone82043033
phone90964159
phone07979
phone110, phone112, phone113
enddocument

If you want to remove spaces from the input, you have to give up expandability:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandphonem
nebu_phone:n #1
tl_new:N l__nebu_phone_tl
cs_new_protected:Nn nebu_phone:n
tl_set:Nn l__nebu_phone_tl #1
tl_remove_all:Nn l__nebu_phone_tl ~
int_compare:nTF tl_count:N l__nebu_phone_tl = 8
__nebu_phone_eight:V l__nebu_phone_tl
tl_use:N l__nebu_phone_tl
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_generate_variant:Nn __nebu_phone_eight:n V
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone232 76 011
phone40 44 30 33
phone820 430 33
phone90964159
phone079 79
phone110, phone112, phone113
enddocument
The output is the same.
add a comment |
Branch according to the length of the argument: if it is eight digit long, branch with respect to the first digit; otherwise print the argument.
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandphonem
nebu_phone:n #1
cs_new:Nn nebu_phone:n
int_compare:nTF tl_count:n #1 = 8
__nebu_phone_eight:n #1
#1
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone23276011
phone40443033
phone82043033
phone90964159
phone07979
phone110, phone112, phone113
enddocument

If you want to remove spaces from the input, you have to give up expandability:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandphonem
nebu_phone:n #1
tl_new:N l__nebu_phone_tl
cs_new_protected:Nn nebu_phone:n
tl_set:Nn l__nebu_phone_tl #1
tl_remove_all:Nn l__nebu_phone_tl ~
int_compare:nTF tl_count:N l__nebu_phone_tl = 8
__nebu_phone_eight:V l__nebu_phone_tl
tl_use:N l__nebu_phone_tl
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_generate_variant:Nn __nebu_phone_eight:n V
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone232 76 011
phone40 44 30 33
phone820 430 33
phone90964159
phone079 79
phone110, phone112, phone113
enddocument
The output is the same.
add a comment |
Branch according to the length of the argument: if it is eight digit long, branch with respect to the first digit; otherwise print the argument.
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandphonem
nebu_phone:n #1
cs_new:Nn nebu_phone:n
int_compare:nTF tl_count:n #1 = 8
__nebu_phone_eight:n #1
#1
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone23276011
phone40443033
phone82043033
phone90964159
phone07979
phone110, phone112, phone113
enddocument

If you want to remove spaces from the input, you have to give up expandability:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandphonem
nebu_phone:n #1
tl_new:N l__nebu_phone_tl
cs_new_protected:Nn nebu_phone:n
tl_set:Nn l__nebu_phone_tl #1
tl_remove_all:Nn l__nebu_phone_tl ~
int_compare:nTF tl_count:N l__nebu_phone_tl = 8
__nebu_phone_eight:V l__nebu_phone_tl
tl_use:N l__nebu_phone_tl
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_generate_variant:Nn __nebu_phone_eight:n V
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone232 76 011
phone40 44 30 33
phone820 430 33
phone90964159
phone079 79
phone110, phone112, phone113
enddocument
The output is the same.
Branch according to the length of the argument: if it is eight digit long, branch with respect to the first digit; otherwise print the argument.
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandphonem
nebu_phone:n #1
cs_new:Nn nebu_phone:n
int_compare:nTF tl_count:n #1 = 8
__nebu_phone_eight:n #1
#1
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone23276011
phone40443033
phone82043033
phone90964159
phone07979
phone110, phone112, phone113
enddocument

If you want to remove spaces from the input, you have to give up expandability:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandphonem
nebu_phone:n #1
tl_new:N l__nebu_phone_tl
cs_new_protected:Nn nebu_phone:n
tl_set:Nn l__nebu_phone_tl #1
tl_remove_all:Nn l__nebu_phone_tl ~
int_compare:nTF tl_count:N l__nebu_phone_tl = 8
__nebu_phone_eight:V l__nebu_phone_tl
tl_use:N l__nebu_phone_tl
cs_new:Nn __nebu_phone_eight:n
str_case_e:nnF tl_head:n #1
4 __nebu_phone_iii_ii_iii:nnnnnnnn #1
8 __nebu_phone_iii_ii_iii:nnnnnnnn #1
9 __nebu_phone_iii_ii_iii:nnnnnnnn #1
__nebu_phone_ii:nnnnnnnn #1
cs_generate_variant:Nn __nebu_phone_eight:n V
cs_new:Nn __nebu_phone_iii_ii_iii:nnnnnnnn #1#2#3nobreakspace#4#5nobreakspace#6#7#8
cs_new:Nn __nebu_phone_ii:nnnnnnnn #1#2nobreakspace#3#4nobreakspace#5#6nobreakspace#7#8
ExplSyntaxOff
begindocument
phone232 76 011
phone40 44 30 33
phone820 430 33
phone90964159
phone079 79
phone110, phone112, phone113
enddocument
The output is the same.
answered 5 hours ago
egregegreg
762k90 gold badges1990 silver badges3338 bronze badges
762k90 gold badges1990 silver badges3338 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f504040%2fsplit-phone-numbers-into-groups-based-on-first-digit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What about numbers with more than 8 digits?
– Marijn
11 hours ago