简体   繁体   中英

java regex extract not working?

What am i missing??

This is the 'dataStoreConstructor' I am passing to the method code below -

url='https://www.salesforce.com/services/Soap/u/26.0',corpnet_prodnet='Corpnet'


  public void registerDataStoreInRepository(String dataStoreConstructor) throws DragonException{
    String constructorPattern = "url='([^']*?)',corpnet_prodnet='([^']*?)'";
    System.out.println(constructorPattern);
    System.out.println(dataStoreConstructor);
    Pattern pattern = Pattern.compile(constructorPattern);
    Matcher matcher = pattern.matcher(dataStoreConstructor);

    SalesforceDataStore sfDataStore = new SalesforceDataStore.Builder(this, matcher.group(1), matcher.group(0)).build();
  };


This is the output I see -

url='([^']*?)',corpnet_prodnet='([^']*?)'Exception in thread "main" 
url='https://www.salesforce.com/services/Soap/u/26.0',corpnet_prodnet='Corpnet'
java.lang.IllegalStateException: No match found
    at java.util.regex.Matcher.group(Matcher.java:485)
    at com.dragon.dictionary.salesforce.SalesforcePlatform.registerDataStoreInRepository(SalesforcePlatform.java:63)

Regex is fine. Just call :

matcher.find() 

once before calling matcher.group() .Your problem will be solved.

Use this:

if (matcher.find()) {
    SalesforceDataStore sfDataStore = new SalesforceDataStore.Builder(this, matcher.group(1), matcher.group(0)).build();
}

You've forgotten to tell the matcher to go and find something :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM