2010-11-16 8 views

risposta

33

No.

Check source code for verification

soluzione: pratica La sua non è standard, ma è possibile utilizzare questo risultato ottenere.

Aggiornamento:

CharSequence inputStr = "abcabcab283c"; 
    String patternStr = "[1-9]{3}"; 
    Pattern pattern = Pattern.compile(patternStr); 
    Matcher matcher = pattern.matcher(inputStr); 
    if(matcher.find()){ 

    System.out.println(matcher.start());//this will give you index 
    } 

O

Regex r = new Regex("YOURREGEX"); 

// search for a match within a string 
r.search("YOUR STRING YOUR STRING"); 

if(r.didMatch()){ 
// Prints "true" -- r.didMatch() is a boolean function 
// that tells us whether the last search was successful 
// in finding a pattern. 
// r.left() returns left String , string before the matched pattern 
int index = r.left().length(); 
} 
+0

C'è qualche soluzione per implementare questo? – Roshan

+0

@ org.life.java - Che libreria stai usando qui? Regex non è nelle librerie java standard. –

+0

@Stephen C Grazie, l'ho seguito su http://www.javaregex.com/ Non sono sicuro che sia affidabile o meno. Non l'ho usato personalmente, ho imparato dal suo tutorial –

27

Si tratta di un approccio in due fasi. Innanzitutto, trova una corrispondenza per il tuo pattern, quindi (secondo) usa Matcher#start per ottenere la posizione della stringa corrispondente nella stringa del contenuto.

Pattern p = Pattern.compile(myMagicPattern); // insert your pattern here 
Matcher m = p.matcher(contentString); 
if (m.find()) { 
    int position = m.start(); 
} 
Problemi correlati