I am currently retrieving the Object Number from a DOORS script, but I need to get just a portion of it. I want to get a substring from the first character to the last index of the hyphen. Since I don't know the exact positions of the hyphens or how many there may be, how do I retrieve everything from the first character to the position of the last hyphen? I've seen that in order to get a substring, you would use strVar[firstCharPos, lastCharPos], but how would I determine the lastCharPos value? I've been trying to use the JavaScript functions of substring, indexOf and lastIndexOf, but these are apparently not the proper way to get the substrings. What is the proper way?
Chris Christopher Cote - Mon Oct 31 11:32:25 EDT 2016 |
Re: Retrieving a substring up to last index of a character Look at matches. |
Re: Retrieving a substring up to last index of a character
string section(Object o) {
/*
match - returns word style section numbers
*/
Regexp sectionNum = regexp2 "([0-9]+(\\.[1-9])*)+"
string objNum = number(o)
if ( sectionNum objNum ) {
objNum = objNum[match 0]
}
delete sectionNum
return objNum
} // section
|
Re: Retrieving a substring up to last index of a character Thank you folks. I took a look at the "matches" function description in the reference manual. I can understand how this would give you whether or not there is a hyphen in the string. It looks like the "match" function would be more like what I want. However, how would I get the last match if there are 2 or more hyphens in the string. For the most part, the object_number will have only 1 hyphen, but there are a few that are similar to "0-1.0-1.0-4.0-2" and I would want to get "0-1.0-1.0-4.0".
Chris Cote |
Re: Retrieving a substring up to last index of a character Christopher Cote - Tue Nov 01 10:34:11 EDT 2016 Thank you folks. I took a look at the "matches" function description in the reference manual. I can understand how this would give you whether or not there is a hyphen in the string. It looks like the "match" function would be more like what I want. However, how would I get the last match if there are 2 or more hyphens in the string. For the most part, the object_number will have only 1 hyphen, but there are a few that are similar to "0-1.0-1.0-4.0-2" and I would want to get "0-1.0-1.0-4.0".
Chris Cote //EZmethod requiring no thought
/*Prints 0-1.0-1.0-4.0 */
int LASTDASH(string s)
int i = LASTDASH(s ) |
Re: Retrieving a substring up to last index of a character DOORSHAM - Tue Nov 01 12:58:50 EDT 2016 //EZmethod requiring no thought
/*Prints 0-1.0-1.0-4.0 */
int LASTDASH(string s)
int i = LASTDASH(s ) By the way, I just remebered that the findPlainText function has a "reverse" parameter which allows searching from the end of the string. So for completeness sake I would add the solution:
string s = "Hallo - Du"
int index = 0, dummy = 0
if (findPlainText(s, "-", index, dummy /* length */, true, true /* reverse */)) {
print "Found at index " index "\n";
} else {
print "Not found.\n"
}
Regards, Mathias |
Re: Retrieving a substring up to last index of a character Mathias Mamsch - Mon Nov 07 03:26:12 EST 2016 By the way, I just remebered that the findPlainText function has a "reverse" parameter which allows searching from the end of the string. So for completeness sake I would add the solution:
string s = "Hallo - Du"
int index = 0, dummy = 0
if (findPlainText(s, "-", index, dummy /* length */, true, true /* reverse */)) {
print "Found at index " index "\n";
} else {
print "Not found.\n"
}
Regards, Mathias Only problem is findPlainText is not documented -- is it wise to tell users to use undocumented perms.
|
Re: Retrieving a substring up to last index of a character DOORSHAM - Mon Nov 07 06:43:13 EST 2016 Only problem is findPlainText is not documented -- is it wise to tell users to use undocumented perms.
It is documented, I think at least since DOORS 9.4 ? What DOORS Version Help are you using? |
Re: Retrieving a substring up to last index of a character DOORSHAM - Tue Nov 01 12:58:50 EDT 2016 //EZmethod requiring no thought
/*Prints 0-1.0-1.0-4.0 */
int LASTDASH(string s)
int i = LASTDASH(s ) I've just thought of a slightly better solution to my problem:
string s = "0-1.0-1.0-4.0-2"
int LASTDASH(string s) {
int l = length s, j, i=0;
for (j=l; j>0; j--) {
if (s[j] == '-') {
i = j;
break;
}
}
return i
}
int i = LASTDASH(s);
print s[0:i-1];
This method is slightly different in that it starts at the last character and moves backward until it finds a hyphen. When it does, it sets the value of i and breaks out of the loop. Granted, this only saves a few microseconds, but if I had a really long string, it will be able to find it faster.
Chris |
Re: Retrieving a substring up to last index of a character Christopher Cote - Mon May 20 15:24:17 EDT 2019 I've just thought of a slightly better solution to my problem:
string s = "0-1.0-1.0-4.0-2"
int LASTDASH(string s) {
int l = length s, j, i=0;
for (j=l; j>0; j--) {
if (s[j] == '-') {
i = j;
break;
}
}
return i
}
int i = LASTDASH(s);
print s[0:i-1];
This method is slightly different in that it starts at the last character and moves backward until it finds a hyphen. When it does, it sets the value of i and breaks out of the loop. Granted, this only saves a few microseconds, but if I had a really long string, it will be able to find it faster.
Chris If you like regular expressions, why not this way?
// collect everything from the beginning in group #1. Then there must be a hyphen, then there may be more characters which are not a hyphen, up to the end of the string
Regexp upToLastDash = regexp2 "^(.+)\\-[^\\-]*$"
string checkem = "0.3-3.4-4-5555"
if (upToLastDash checkem) {
print checkem[match 1]
}
|
Re: Retrieving a substring up to last index of a character EHcnck - Mon Oct 31 11:51:24 EDT 2016
string section(Object o) {
/*
match - returns word style section numbers
*/
Regexp sectionNum = regexp2 "([0-9]+(\\.[1-9])*)+"
string objNum = number(o)
if ( sectionNum objNum ) {
objNum = objNum[match 0]
}
delete sectionNum
return objNum
} // section
If I have a function that uses a static RegExp, I just define it above the function. They you don't waste time creating/deleting the RegExp. |
Re: Retrieving a substring up to last index of a character Christopher Cote - Mon May 20 15:24:17 EDT 2019 I've just thought of a slightly better solution to my problem:
string s = "0-1.0-1.0-4.0-2"
int LASTDASH(string s) {
int l = length s, j, i=0;
for (j=l; j>0; j--) {
if (s[j] == '-') {
i = j;
break;
}
}
return i
}
int i = LASTDASH(s);
print s[0:i-1];
This method is slightly different in that it starts at the last character and moves backward until it finds a hyphen. When it does, it sets the value of i and breaks out of the loop. Granted, this only saves a few microseconds, but if I had a really long string, it will be able to find it faster.
Chris I think you want this: for (j=l-1; j>=0; j--) { |
Re: Retrieving a substring up to last index of a character llandale - Wed May 22 20:25:43 EDT 2019 I think you want this: for (j=l-1; j>=0; j--) { To answer your question Lou, I'm not very good with creating regular expressions, so I tend to steer away from them. Perhaps when I get better with them, I'll use them more often than looping through the string. As for your last post, I probably should loop all the way to the first character (>=0 vs. >0), but I'm making an assumption that the first character in the string is never a hyphen.
Chris |
Re: Retrieving a substring up to last index of a character Christopher Cote - Thu May 23 08:42:35 EDT 2019 To answer your question Lou, I'm not very good with creating regular expressions, so I tend to steer away from them. Perhaps when I get better with them, I'll use them more often than looping through the string. As for your last post, I probably should loop all the way to the first character (>=0 vs. >0), but I'm making an assumption that the first character in the string is never a hyphen.
Chris I'm terrible with Regular Expressions. I have to spell everthing out and comment all over it, in order for me to make it work. Here's an example. Notice that setting the string and printing it before the "regexp2" command helps tremendously in debugging it.
string plc_sRE_Script_FileParts =
"^" // start of string -
"(" lc_re_strAnyChar "*)" // match 1: Base File Name before Extension -
plc_sRE_Script_Extention // match 2: period, then valid Script extension -
"(" lc_re_strAnyChar "*)" // match 3: Trailing chars that have no Period (and so no Extension) -
"$" // end of string
// EOLs are allowed but make no sense to do so.
// "Greedy" matching.
//print "plc_sRE_Script_FileParts = [" plc_sRE_Script_FileParts "]\n"
Regexp plc_re_Script_FileParts = regexp2(plc_sRE_Script_FileParts)
|