Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2010
    Messaggi
    1

    Java e regex per estrarre campi

    Ciao a tutti,
    sto cercando di estrarre dei campi da una stringa ma ho dei problemi con le regex.
    La stringa con cui sto facendo le prove è:

    "Questa è una prova <code/> e anche <name/> oppure <batch/>"

    Da questa stringa, tramite regex, sto cercando di estrarre tutti i campi delimitati da < e />,
    ho fatto diverse prove ma riesco a prendere sempre e solo l'ultima occorenza (batch) ma non le altre due.... (non è detto che i campi tra parentesi angolari siano solo quei tre)

    Il mio codice è:

    //--initialization Area
    objRegExPattern = Pattern.compile("<(\\w+)/>");

    Matcher objMatcher = objRegExPattern.matcher( p_strPatern );

    while (objMatcher.find()) {
    // Get the matching string
    match = objMatcher.group( 1 );
    System.out.print(match);
    }


    Avete un'idea in merito?



    Ciao a tutti e grazie

    Alessandro

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Java e regex per estrarre campi

    Originariamente inviato da _alex_
    codice:
    objRegExPattern = Pattern.compile("<(\\w+)/>");
    
    Matcher objMatcher = objRegExPattern.matcher( p_strPatern );
    
    while (objMatcher.find()) {
        // Get the matching string
        match = objMatcher.group( 1 );
        System.out.print(match);
    }
    Avete un'idea in merito?
    Ma io non vedo nulla di strano ... la regex è appropriata per quello che devi fare. Con la stringa di prova postata avresti 3 cicli e in ognuno ottieni "code", "name", "batch".
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.