Using regex with startsWith command

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Using regex with startsWith command

Damian
I am trying to match two strings which starts with a matching substring.
For example, I wish to match "Name_dish_field_1_ABC_DEF.tif" with "Name_dish_field_1_EFG_H_I.tif"
but not with "Name_dish_field_2_EFG_H_I.tif"; i.e. the matching strings contains the same string before the 4th occurrence of "_"  character.

I am using a very crude loop at the moment:

//Starting String
Full_string = "Name_dish_field_1_ABC_EFG.tif";

//Match everything before the 4th occurrence of "_"
Character_for_matching = "_";
Match_before_number_of_occurences = 4;

Character_occurence_position = indexOf(Full_string, Character_for_matching);

for(i=1;i<Match_before_number_of_occurences;i++){
Character_occurence_position  = indexOf(Full_string, Character_for_matching, Character_occurence_position+1);
print(Character_occurence_position);
}

//Substring used for matching
Substring_of_interest=substring(Full_string, 0, Character_occurence_position);
print(Substring_of_interest);

//Should match
print(startsWith("Name_dish_field_1_ABC_EFG_GHI.tif", Substring_of_interest));
//Shouldn't match
print(startsWith("Name_dish_field_2_JKL", Substring_of_interest));



but I was wondering if I can do something simpler use regex and startsWith function. Is there someway I can  use "^(?:[^_]*_){3}([^_]*)", it seems to work when I test it using a regex tester http://www.regexr.com/3br9e