function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jos Vervoorn 2Jos Vervoorn 2 

Regex find is true but I get no results ....on timezone substring ...

I'm using substring to extract valid user timezone picklist values using below code. It does match .. but I get to results.
String text= 'Australian Central Standard Time ((South Australia) Australia/Adelaide)';

String RegEx = '(\\w+[/]\\w+[/]?\\w+)||GMT';
Pattern regexPattern = Pattern.compile(regex);
Matcher mymatcher= regexPattern.matcher(text);
If (myMatcher.find()){
  system.debug(LoggingLevel.Info,'*** Match ' + myMatcher.group());
}
What I expect in the log is Australia/Adelaide. The RegEx seems ok ... https://regexr.com/43hi9
Best Answer chosen by Jos Vervoorn 2
Jos Vervoorn 2Jos Vervoorn 2
Found it .. the's a typo in the regex itself. It should have been String RegEx = '(\\w+[/]\\w+[/]?\\w+)|GMT'; .. so I removed the second pipe there.

All Answers

Alain CabonAlain Cabon
Hi,

Your code is correct. Just select the first group like below.
 
List <String>texts= new List<String>{'Australian Central Standard Time ((South Australia) Australia/Adelaide)','Greenwich Mean Time (GMT)'};
String RegEx = '((\\w+[/]\\w+[/]?\\w+)|GMT)';
Pattern regexPattern = Pattern.compile(regex);
for (String text:texts) {
      Matcher mymatcher= regexPattern.matcher(text);
      if (myMatcher.find()){
              system.debug(LoggingLevel.Info,'*** Match [' + myMatcher.group(1) + ']');
      } 
}
Jos Vervoorn 2Jos Vervoorn 2
Found it .. the's a typo in the regex itself. It should have been String RegEx = '(\\w+[/]\\w+[/]?\\w+)|GMT'; .. so I removed the second pipe there.
This was selected as the best answer