Home > Mobile >  How to use a regular string segmentation orderly number?
How to use a regular string segmentation orderly number?

Time:09-25

Some of the string is made up of a number of serial number together, such as

` ` `
1, XXXXX2, XXXXX, XXXX 3, XXXXX
` ` `
How can I get

` ` `
1, XXXXX
2, XXXXX, XXXX
3, XXXXX
` ` `
Such an array? (no language)


I use python regular segmentation,

` ` `
STR="1, aaaaa2, BBBBB, bb, 3 ccccc4, DDDD"
Res=re. Split (' \ d (. *?) \ 'd, STR)
` ` `
The result is


` ` `
[' aaaaa ', 'CCCCC]
How to write to get
[" aaaaa ", "BBBBB ', 'CCCCC', 'DDDD'] (also line with serial number)
` ` `

CodePudding user response:

The split should be regular "\ d +," line
But I don't know will appear abnormal data, such as: 1, aaa2, bbb3, a1234, xxx5, adsa
This data can only use the code in order to distinguish the, solve the problem or from the root, return a json array directly, do not return this extremely easy to cause ambiguity format

CodePudding user response:

Already find a solution to write
The import re
S="1, aaaaa2, BBBBB, bb, 3 ccccc4, DDDD"
Res=re. The.findall (' \ d \ d + 's)
Print (res)
  • Related