1

Trying to extract the text 18-Aug-2019 14:00 form Egypt Today Last Update Time: 18-Aug-2019 14:00 (GMT) and my step was to split at ":" as first step and then do the split " (" section (basically 2 splits ), and 2 splits is not working... can we do this from only one step? thanks

Code trials:

lastupdated1=lastupdated.split("Last Update Time: ")[1]
lastupdated2=lastupdated1.split(" (GMT")[0]

The error is:

2019-08-19 14:54:53.692 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/REGIONAL MARKET NEWS/Verify_whether_news_getting_updated FAILED.
    Reason:
    java.util.regex.PatternSyntaxException: Unclosed group near index 5
     (GMT
        at java_lang_String$split$0.call(Unknown Source)
        at Verify_whether_news_getting_updated.run(Verify_whether_news_getting_updated:41)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
lahimadhe
  • 374
  • 2
  • 16
  • 3
    The error message is clear. In your regular expression `(GMT` there is an unclosed group. i.e. you have started some parentheses and not finished them. I presume you want to match a literal opening parenthesis, so you need to escape it. In java, you need to escape the escape, so use `\\(GMT` instead. – Michael Aug 19 '19 at 09:43
  • https://stackoverflow.com/users/1898563/michael thanks a lot, now working fine. – lahimadhe Aug 19 '19 at 09:47
  • Sure, no problem – Michael Aug 19 '19 at 09:48
  • The key is that `.split` takes a regex. – sp00m Aug 19 '19 at 11:40

1 Answers1

2

You can easily extract the text 18-Aug-2019 14:00 form Egypt Today Last Update Time: 18-Aug-2019 14:00 (GMT) using split() only once passing a and you can use the following solution:

  • Code Block:

    String myNewString = "Egypt Today Last Update Time: 18-Aug-2019 14:00 (GMT)";
    String[] tokens = myNewString.split(": |\\(");
    System.out.println(tokens[1]);
    
  • Console Output:

    18-Aug-2019 14:00
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352