Hello everyone.
I would like to have a function to grep a certain fragment out of a string. Example-Code: input = "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; regEx = replace(input, "[0-9]*:[0-9]*:[0-9]*:[0-9]*", "FOUND"); edge1 = indexOf(input, "DPX:tv.time.code="); edge2 = lastIndexOf(input, "00 |"); if (edge2!=-1 && edge1 < edge2) input = substring(input, edge1, edge2); print("\\Clear"); print("regex replace: "+regEx); print("extracted string: "+input ); I need this fragment: DPX:tv.time.code=10:28:59:00 maybe like this.. input= "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; mod= stringGrep(input, "DPX:tv.time.code=[0-9]*:[0-9]*:[0-9]*:[0-9]*"); Is there a workaround/way to achieve this? In my opinion it would be great to make a grep for specific fragment and to get this in return or the line in which a fragment was found. By multiple occurrences a resulting list would be nice. Regards, Rainer -- Rainer M. Engel, Dipl. Digital Artist scientific|Media GbR Pichelsdorferstr. 143 D-13595 Berlin |
Hi Rainer,
there is another alternative: input = "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; result = "<no timecode found>"; parts=split(input, "|"); for (i=0; i<parts.length; i++) if (matches(parts[i], ".*DPX:tv.time.code.*")) result = parts[i]; print(result); By the way, this keeps the blanks at the beginning and end of the string. A String trim macro function (removing leasing and trailing whitespace) would be handy. Michael ________________________________________________________________ On Feb 2, 2012, at 18:58, Rainer M. Engel wrote: > Hello everyone. > > I would like to have a function to grep a certain fragment out of a string. > > Example-Code: > input = "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; > regEx = replace(input, "[0-9]*:[0-9]*:[0-9]*:[0-9]*", "FOUND"); > edge1 = indexOf(input, "DPX:tv.time.code="); > edge2 = lastIndexOf(input, "00 |"); > if (edge2!=-1 && edge1 < edge2) > input = substring(input, edge1, edge2); > print("\\Clear"); > print("regex replace: "+regEx); > print("extracted string: "+input ); > > > I need this fragment: > DPX:tv.time.code=10:28:59:00 > > > maybe like this.. > input= "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; > mod= stringGrep(input, "DPX:tv.time.code=[0-9]*:[0-9]*:[0-9]*:[0-9]*"); > > > Is there a workaround/way to achieve this? > In my opinion it would be great to make a grep for specific fragment and > to get this in return or the line in which a fragment was found. By > multiple occurrences a resulting list would be nice. > > > Regards, > Rainer > > > -- > Rainer M. Engel, Dipl. Digital Artist > scientific|Media GbR > Pichelsdorferstr. 143 > D-13595 Berlin |
Hey Michael,
thank you for this tip. This way is good starting point.. Rainer Am 03.02.2012 19:00, schrieb Michael Schmid: > Hi Rainer, > > there is another alternative: > > input = "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; > > result = "<no timecode found>"; > parts=split(input, "|"); > for (i=0; i<parts.length; i++) > if (matches(parts[i], ".*DPX:tv.time.code.*")) > result = parts[i]; > > print(result); > > > By the way, this keeps the blanks at the beginning and end of the string. > > A String trim macro function (removing leasing and trailing whitespace) would be handy. > > > Michael > ________________________________________________________________ > On Feb 2, 2012, at 18:58, Rainer M. Engel wrote: > >> Hello everyone. >> >> I would like to have a function to grep a certain fragment out of a string. >> >> Example-Code: >> input = "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; >> regEx = replace(input, "[0-9]*:[0-9]*:[0-9]*:[0-9]*", "FOUND"); >> edge1 = indexOf(input, "DPX:tv.time.code="); >> edge2 = lastIndexOf(input, "00 |"); >> if (edge2!=-1 && edge1 < edge2) >> input = substring(input, edge1, edge2); >> print("\\Clear"); >> print("regex replace: "+regEx); >> print("extracted string: "+input ); >> >> >> I need this fragment: >> DPX:tv.time.code=10:28:59:00 >> >> >> maybe like this.. >> input= "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; >> mod= stringGrep(input, "DPX:tv.time.code=[0-9]*:[0-9]*:[0-9]*:[0-9]*"); >> >> >> Is there a workaround/way to achieve this? >> In my opinion it would be great to make a grep for specific fragment and >> to get this in return or the line in which a fragment was found. By >> multiple occurrences a resulting list would be nice. >> >> >> Regards, >> Rainer >> >> >> -- >> Rainer M. Engel, Dipl. Digital Artist >> scientific|Media GbR >> Pichelsdorferstr. 143 >> D-13595 Berlin > |
In reply to this post by Michael Schmid
I would like to second Michael's wish for a trim function but I want to
show some other difficulties. //MACRO-Example: var1 = "74-bla-7472394-what-I-want27-idasidaid-23u83422-42342ffe"; var2 = "28930-blubb-402828603-what-I-want48-sjif-zhafhadgqw-fdsa"; rep1 = replace(var1, "what-I-want[0-9]*", "##FOUND##"); rep2 = replace(var2, "what-I-want[0-9]*", "##FOUND##"); print("\\Clear"); print("Replaced 1: "+rep1); print("Replaced 2: "+rep2); //End I would like to have a counterpart for the replace function. It is so comfortable to replace what I want but it seems to be uncomfortable, if not impossible to grep/keep exactly the same. Speaking for the matches function it is basically the same. I'm able to check/filter strings conforming to regular expressions, but I'm not able to get exact these substrings. At least not in one single step with the same expression, what would be great. Maybe I missed something very obvious. I don't know Regards, Rainer Am 03.02.2012 19:00, schrieb Michael Schmid: > Hi Rainer, > > there is another alternative: > > input = "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; > > result = "<no timecode found>"; > parts=split(input, "|"); > for (i=0; i<parts.length; i++) > if (matches(parts[i], ".*DPX:tv.time.code.*")) > result = parts[i]; > > print(result); > > > By the way, this keeps the blanks at the beginning and end of the string. > > A String trim macro function (removing leasing and trailing whitespace) would be handy. > > > Michael > ________________________________________________________________ > On Feb 2, 2012, at 18:58, Rainer M. Engel wrote: > >> Hello everyone. >> >> I would like to have a function to grep a certain fragment out of a string. >> >> Example-Code: >> input = "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; >> regEx = replace(input, "[0-9]*:[0-9]*:[0-9]*:[0-9]*", "FOUND"); >> edge1 = indexOf(input, "DPX:tv.time.code="); >> edge2 = lastIndexOf(input, "00 |"); >> if (edge2!=-1 && edge1 < edge2) >> input = substring(input, edge1, edge2); >> print("\\Clear"); >> print("regex replace: "+regEx); >> print("extracted string: "+input ); >> >> >> I need this fragment: >> DPX:tv.time.code=10:28:59:00 >> >> >> maybe like this.. >> input= "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; >> mod= stringGrep(input, "DPX:tv.time.code=[0-9]*:[0-9]*:[0-9]*:[0-9]*"); >> >> >> Is there a workaround/way to achieve this? >> In my opinion it would be great to make a grep for specific fragment and >> to get this in return or the line in which a fragment was found. By >> multiple occurrences a resulting list would be nice. >> >> >> Regards, >> Rainer >> >> >> -- >> Rainer M. Engel, Dipl. Digital Artist >> scientific|Media GbR >> Pichelsdorferstr. 143 >> D-13595 Berlin |
Hi Rainer,
it seems that Java's Matcher.group() is roughly what you need? http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html I have not ventured much into Javascript for macros, but it should be doable via javascript. Or does anyone else out there have a better idea? In case of adding something like Matcher.group() to the macro language, (e.g. getMatches(string, regexp): what if there are more matches? Should it return an array? Michael ________________________________________________________________ On Feb 16, 2012, at 10:54, Rainer M. Engel wrote: > I would like to second Michael's wish for a trim function but I want to > show some other difficulties. > > //MACRO-Example: > > var1 = "74-bla-7472394-what-I-want27-idasidaid-23u83422-42342ffe"; > var2 = "28930-blubb-402828603-what-I-want48-sjif-zhafhadgqw-fdsa"; > > rep1 = replace(var1, "what-I-want[0-9]*", "##FOUND##"); > rep2 = replace(var2, "what-I-want[0-9]*", "##FOUND##"); > > print("\\Clear"); > print("Replaced 1: "+rep1); > print("Replaced 2: "+rep2); > > //End > > I would like to have a counterpart for the replace function. It is so > comfortable to replace what I want but it seems to be uncomfortable, if > not impossible to grep/keep exactly the same. > > Speaking for the matches function it is basically the same. I'm able to > check/filter strings conforming to regular expressions, but I'm not able > to get exact these substrings. At least not in one single step with the > same expression, what would be great. > > Maybe I missed something very obvious. I don't know > > Regards, > Rainer > > > > Am 03.02.2012 19:00, schrieb Michael Schmid: >> Hi Rainer, >> >> there is another alternative: >> >> input = "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; >> >> result = "<no timecode found>"; >> parts=split(input, "|"); >> for (i=0; i<parts.length; i++) >> if (matches(parts[i], ".*DPX:tv.time.code.*")) >> result = parts[i]; >> >> print(result); >> >> >> By the way, this keeps the blanks at the beginning and end of the string. >> >> A String trim macro function (removing leasing and trailing whitespace) would be handy. >> >> >> Michael >> ________________________________________________________________ >> On Feb 2, 2012, at 18:58, Rainer M. Engel wrote: >> >>> Hello everyone. >>> >>> I would like to have a function to grep a certain fragment out of a string. >>> >>> Example-Code: >>> input = "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; >>> regEx = replace(input, "[0-9]*:[0-9]*:[0-9]*:[0-9]*", "FOUND"); >>> edge1 = indexOf(input, "DPX:tv.time.code="); >>> edge2 = lastIndexOf(input, "00 |"); >>> if (edge2!=-1 && edge1 < edge2) >>> input = substring(input, edge1, edge2); >>> print("\\Clear"); >>> print("regex replace: "+regEx); >>> print("extracted string: "+input ); >>> >>> >>> I need this fragment: >>> DPX:tv.time.code=10:28:59:00 >>> >>> >>> maybe like this.. >>> input= "DPX:file.version=V1.0 | DPX:tv.time.code=10:28:59:00 | DPX"; >>> mod= stringGrep(input, "DPX:tv.time.code=[0-9]*:[0-9]*:[0-9]*:[0-9]*"); >>> >>> >>> Is there a workaround/way to achieve this? >>> In my opinion it would be great to make a grep for specific fragment and >>> to get this in return or the line in which a fragment was found. By >>> multiple occurrences a resulting list would be nice. >>> >>> >>> Regards, >>> Rainer >>> >>> >>> -- >>> Rainer M. Engel, Dipl. Digital Artist >>> scientific|Media GbR >>> Pichelsdorferstr. 143 >>> D-13595 Berlin |
Some asked for a string space trim function.
This is quite simple: a=" 234 tyu 454 "; while(startsWith(a," ") ){ a=substring(a,1); } while(endsWith(a," ") ){ a=substring(a,0, lengthOf(a)-1); } This will trim leading and trailing spaces. Cheers G |
Am 16.02.2012 13:09, schrieb Gabriel Landini:
> Some asked for a string space trim function. > This is quite simple: > > a=" 234 tyu 454 "; > > while(startsWith(a," ") ){ > a=substring(a,1); > } > while(endsWith(a," ") ){ > a=substring(a,0, lengthOf(a)-1); > } > Thanks Gabriel.. I came across another approach. a = " 234 tyu 454 "; a = replace(a, "\\s*$", ""); //removes trailing whitespaces a = replace(a, "^\\s*", ""); //removes leading whitespaces getMatches, as Michael pointed out, would be nice. An array would be a good idea by more than one match. Its length can be retrieved easy. Regards, Rainer -- Rainer M. Engel, Dipl. Digital Artist scientific|Media GbR Pichelsdorferstr. 143 D-13595 Berlin |
Free forum by Nabble | Edit this page |