0
votes

Diviser une chaîne en sous-tableaux

J'ai une chaîne comme: xxx

Il n'y a pas de délimitation entre les valeurs telles que OG, OB, est etc. Je veux diviser le tableau prêté à base de "=" de sorte que og, Les champs ob ... Les champs sont inclus dans des divisions résultantes. J'ai besoin de traiter davantage ces champs pour les sous-classés.


1 commentaires

Vous pouvez utiliser Regex pour résoudre ce problème.


4 Réponses :


0
votes
 String OG = "ACC-0000000009| AMBORFFA KIRI|P.O.BOX 1FAF6GPO,GPO,FG/FFERER ";
    String[] split = OG.split(",");//以逗号为分隔符转换为字符串数组
    for (int i = 0; i < split.length; i++) {//遍历数组
        System.out.println(split[i]);

0 commentaires

1
votes

quelque chose comme ça? (Code SCALA)

val str =
  "OG=ACC-0000000009| AMBORFFA KIRI|P.O.BOX 1FAF6GPO,GPO,FG/FFERER Transaction Amount= 1223|546SD|376KL OB=XXXX-XXCC|ABC|14332 X HWay|Vica |MNSJD      IS=BIC-dfsgdf|asas nduf|142 ERRET ERT RET|ERTERT Island|ERTERT     BF=ACC-0000013417711DD028|534 DFG ION|ONE DALLAERRS CENTER RR N.|     ERTERT, SUITE 1300, DRRALLAS,|Pb, 75201/ PBB      DT=GREAT CHART|0000FGHFGGL028434  "

str.split(raw"\b(?=Transaction Amount=|\S\S=)")
   .foldLeft(Map.empty[String,Array[String]]){
     case (m,s) => val (k,v) = s.splitAt(s.indexOf("="))
                   m + (k -> v.tail.split("\\|"))
   }
//HashMap(OG -> Array(ACC-0000000009, " AMBORFFA KIRI", "P.O.BOX 1FAF6GPO,GPO,FG/FFERER ")
//      , OB -> Array(XXXX-XXCC, ABC, 14332 X HWay, "Vica ", "MNSJD      ")
//      , Transaction Amount -> Array(" 1223", 546SD, "376KL ")
//      , DT -> Array(GREAT CHART, "0000FGHFGGL028434  ")
//      , IS -> Array(BIC-dfsgdf, asas nduf, 142 ERRET ERT RET, ERTERT Island, "ERTERT     ")
//      , BF -> Array(ACC-0000013417711DD028, 534 DFG ION, ONE DALLAERRS CENTER RR N., "     ERTERT, SUITE 1300, DRRALLAS,", "Pb, 75201/ PBB      "))


1 commentaires

Merci pour la solution !! Besoin de mettre à jour la solution pour une nouvelle colonne ajoutée => Montant de transaction = 1223 | 546SD | 376kl, pouvez-vous aider à cela?



0
votes

Voici une solution SCALA REGEX:

val data = "OG=ACC-0000000009| AMBORFFA KIRI|P.O.BOX 1FAF6GPO,GPO,FG/FFERER..."

data.split("""(?=\w\w=)""")


0 commentaires

1
votes

Regex pourrait être l'une des solutions. Mais je recommande d'utiliser Délimiteur si vous le pouvez.

C'est ma solution, mais je ne sais pas si cela fonctionne dans tous les cas: P>

public static void main(String[] args){

    String text = "OG=ACC-0000000009| AMBORFFA KIRI|P.O.BOX 1FAF6GPO,GPO,FG/FFERER      OB=XXXX-XXCC|ABC|14332 X HWay|Vica |MNSJD      IS=BIC-dfsgdf|asas nduf|142 ERRET ERT RET|ERTERT Island|ERTERT     BF=ACC-0000013417711DD028|534 DFG ION|ONE DALLAERRS CENTER RR N.|     ERTERT, SUITE 1300, DRRALLAS,|Pb, 75201/ PBB      DT=GREAT CHART|0000FGHFGGL028434";

    //Regex for field
    String regexField = "(?<field>[A-Z]+)(:?[=])";

    Pattern pattern = Pattern.compile(regexField);
    Matcher matcher = pattern.matcher(text);

    //extract fields names
    List<String> fields = new ArrayList<>();
    while(matcher.find()){
        fields.add(matcher.group("field"));
    }

    //extract values using split and regex for fields
    List<String> values = Arrays.stream(text.split(regexField))
                                .map(String::trim)
                                .filter(e -> !e.isEmpty())
                                .collect(Collectors.toList());

    //group fields and values
    Map<String, String> data = new HashMap<>();
    if(fields.size() == values.size()){

        for(int i = 0; i < fields.size(); i++){
            data.put(fields.get(i), values.get(i));
        }

    }else{

        System.out.println("Size are different. Something is not good.");

    }

    data.forEach((k, v) -> System.out.println(k + " -> " + v));

}


0 commentaires