groovy & grails - lecture 3

1,116 views

Published on

Eclipse tips
The Groovy truth
Control structures
Regular expressions, a few step forwards
Introduction to classes

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,116
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
26
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • horizontal learning\n
  • read the function shortcuts and try to memorize your most commons\n
  • read the function shortcuts and try to memorize your most commons\n
  • read the function shortcuts and try to memorize your most commons\n
  • read the function shortcuts and try to memorize your most commons\n
  • \n
  • \n
  • \n
  • \n
  • ask for perl before bullet 2\n
  • ask for perl before bullet 2\n
  • \n
  • if you don’t write the break, next line will be evaluated!!\n
  • if you don’t write the break, next line will be evaluated!!\n
  • take care if params.x is 0 or ‘’\ndepends on the context\nparams.x?:42 + 23 != (params.x?:42) + 23\n
  • take care if params.x is 0 or ‘’\ndepends on the context\nparams.x?:42 + 23 != (params.x?:42) + 23\n
  • take care if params.x is 0 or ‘’\ndepends on the context\nparams.x?:42 + 23 != (params.x?:42) + 23\n
  • take care if params.x is 0 or ‘’\ndepends on the context\nparams.x?:42 + 23 != (params.x?:42) + 23\n
  • \n
  • \n
  • \n
  • \n
  • sometimes, while(true) , then break exit for the condition\n
  • sometimes, while(true) , then break exit for the condition\n
  • \n
  • rich scripting => TIMTOWTDI\nmore than one manner: choose your preferred, but be able to read other people code\n
  • rich scripting => TIMTOWTDI\nmore than one manner: choose your preferred, but be able to read other people code\n
  • rich scripting => TIMTOWTDI\nmore than one manner: choose your preferred, but be able to read other people code\n
  • rich scripting => TIMTOWTDI\nmore than one manner: choose your preferred, but be able to read other people code\n
  • most of what is seen here was already discussed in winter => go fast\n
  • most of what is seen here was already discussed in winter => go fast\n
  • most of what is seen here was already discussed in winter => go fast\n
  • most of what is seen here was already discussed in winter => go fast\n
  • most of what is seen here was already discussed in winter => go fast\n
  • most of what is seen here was already discussed in winter => go fast\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • always prefer \\s+ to ‘ ‘ when you suspect tab\ninverse of split is join\n\n
  • always prefer \\s+ to ‘ ‘ when you suspect tab\ninverse of split is join\n\n
  • always prefer \\s+ to ‘ ‘ when you suspect tab\ninverse of split is join\n\n
  • always prefer \\s+ to ‘ ‘ when you suspect tab\ninverse of split is join\n\n
  • always prefer \\s+ to ‘ ‘ when you suspect tab\ninverse of split is join\n\n
  • going through title for line length\n
  • going through title for line length\n
  • going through title for line length\n
  • idem $1, $2 ... in perl\n
  • idem $1, $2 ... in perl\n
  • idem $1, $2 ... in perl\n
  • groovy & grails - lecture 3

    1. 1. Groovy: Efficiency Oriented ProgrammingLecture 3Master Proteomics & Bioinformatics - University of GenevaAlexandre Masselot - summer 2011
    2. 2. Contents• Eclipse tips• The Groovy truth• Control structures• Regular expressions, a few step forwards• Introduction to classes
    3. 3. A couple of eclipse tips• <ctrl>-F : search text in file, replace; regular expressions possible,
    4. 4. A couple of eclipse tips• <ctrl>-F : search text in file, replace; regular expressions possible,• <ctrl>-J + word : search text in file, without opening a popup,
    5. 5. A couple of eclipse tips• <ctrl>-F : search text in file, replace; regular expressions possible,• <ctrl>-J + word : search text in file, without opening a popup,• <ctrl>-R + filena : jump to the given filename directly. If multiple files correspond to the name entered, they are proposed,
    6. 6. A couple of eclipse tips• <ctrl>-F : search text in file, replace; regular expressions possible,• <ctrl>-J + word : search text in file, without opening a popup,• <ctrl>-R + filena : jump to the given filename directly. If multiple files correspond to the name entered, they are proposed,• select text + <ctrl>-7 [ right button -> source -> toggle comment] : passed the whole selection in or out comment mode (prepend // to lines)
    7. 7. A couple of linux (shell) tipshistory to list the last console command history history | grep psql //the last command with “psql”
    8. 8. A couple of linux (shell) tipshistory to list the last console command history history | grep psql //the last command with “psql”<ctrl>-R to find last command <ctrl>-r + ls //jump to last command with “ls” //call <ctrl>-r multiple times
    9. 9. A couple of linux (shell) tipshistory to list the last console command history history | grep psql //the last command with “psql”<ctrl>-R to find last command <ctrl>-r + ls //jump to last command with “ls” //call <ctrl>-r multiple timestab for completion of the current word in plenty of context fin<tab> //all command start with “fin”
    10. 10. A couple of linux (shell) tipshistory to list the last console command history history | grep psql //the last command with “psql”<ctrl>-R to find last command <ctrl>-r + ls //jump to last command with “ls” //call <ctrl>-r multiple timestab for completion of the current word in plenty of context fin<tab> //all command start with “fin”ls to list files ls -lrt //long format, sorted reverse on time ls -lh //size in human format (1.2M, 3.5G ...) ls -lhrt | tail //10 most recent 10
    11. 11. Control structures : the Groovy truth• In many situations, a boolean (true or false) must be deduced from the context
    12. 12. Control structures : the Groovy truth• In many situations, a boolean (true or false) must be deduced from the context• In perl, for example, false can be deduced from • undef • empty string ‘’ •0
    13. 13. Groovy truth (cont’d)
    14. 14. Groovy truth (cont’d)In Groovy, several condition produce false (and opposite produces true) assert !false //boolean value assert !(‘a’ =~ /b/) //regular expression assert ![] //empty list assert ![:] //empty map assert !‘‘ //empty string assert !0 //zero assert !null //null pointer assert true assert a=~/./ assert [1] assert [a:1] assert ‘my funny valentine’ assert 1 assert new Object()
    15. 15. conditional constructLike in perl, the classic if/else construction if(x<10){ ... }else if(x<20){ ... }else{ ... }
    16. 16. conditional constructLike in perl, the classic if/else construction if(x<10){ ... }else if(x<20){ ... }else{ ... }Or the switch switch(x){ case 1 : println ‘one’; break case 2..10 : println ‘between 2 and 10’; break default : println ‘anything else’ }
    17. 17. Single line conditional statementLike in other language, the (...)?(...):(...) boolean cond=true int x if(cond){ x=10 } else { x=42 }
    18. 18. Single line conditional statementLike in other language, the (...)?(...):(...) boolean cond=true int x if(cond){ x=10 } else { x=42 }Can be replaced by int x=cond?10:42
    19. 19. Single line conditional statementLike in other language, the (...)?(...):(...) boolean cond=true int x if(cond){ x=10 } else { x=42 }Can be replaced by int x=cond?10:42In the same idea, a default value can be set if(params.x){ xVal=params.x } else { xVal=42 }
    20. 20. Single line conditional statementLike in other language, the (...)?(...):(...) boolean cond=true int x if(cond){ x=10 } else { x=42 }Can be replaced by int x=cond?10:42In the same idea, a default value can be set if(params.x){ xVal=params.x } else { xVal=42 }Can be replaced with the ?: (the equivalent of perl’s ||) xVal=params.x?:42
    21. 21. Single line conditional statement (cont’d)Imagine we want to sort elements on last name, and if they are equals, onthe first name def nameList=[[first:Harry, last:Potter], [first:Ron, last:Weasley], [first:Fred, last:Weasley], [first:Georges, last:Weasley], , [first:Hermione, last:Granger] ]
    22. 22. Single line conditional statement (cont’d)Imagine we want to sort elements on last name, and if they are equals, onthe first name def nameList=[[first:Harry, last:Potter], [first:Ron, last:Weasley], [first:Fred, last:Weasley], [first:Georges, last:Weasley], , [first:Hermione, last:Granger] ]The first “long” way: nameList.sort{o1, o2 -> if(o1.last == o2.last){ return o1.first <=> o2.first // <=> the spaceship }else{ // operator return o1.last <=> o2.last // compares elements } }.each{println “$it.first $it.last”}
    23. 23. Single line conditional statement (cont’d)Imagine we want to sort elements on last name, and if they are equals, onthe first name def nameList=[[first:Harry, last:Potter], [first:Ron, last:Weasley], [first:Fred, last:Weasley], [first:Georges, last:Weasley], , [first:Hermione, last:Granger] ]The first “long” way: nameList.sort{o1, o2 -> if(o1.last == o2.last){ return o1.first <=> o2.first // <=> the spaceship }else{ // operator return o1.last <=> o2.last // compares elements } }.each{println “$it.first $it.last”}Or the more compact manner: (o1.last <=> o2.last)?:(o1.first <=> o2.first)
    24. 24. While loopwhile(cond) is evaluated until the condition is false List l=[] x=5 while(x--){ l << x } assert l == [4, 3, 2, 1, 0]
    25. 25. While loopwhile(cond) is evaluated until the condition is false List l=[] x=5 while(x--){ l << x } assert l == [4, 3, 2, 1, 0]A loop scope can be exited with a break List l=[] x=5 while(x--){ l << x if(x==3) break // NB a single line statement, no {} } assert l == [4, 3]
    26. 26. Loop: breaking the flow (cont’d)It can be useful to jump directly to the next iteration, without evaluation theremaining part of the loop, with the continue statement int x = 10 def list=[] while(x--){ if (x%2) continue // return to the while evaluation if (x<4) break // exit list << x } assert list == [8, 6, 4]
    27. 27. for loopAlthough closure makes it less critical, the for statement allows to walkthrough a collection String t=’’ for(String s in ‘a’..’d’){ t+=s } assert t == ‘abcd’
    28. 28. for loopAlthough closure makes it less critical, the for statement allows to walkthrough a collection String t=’’ for(String s in ‘a’..’d’){ t+=s } assert t == ‘abcd’It is equivalent to (‘a’..’d’).each{t+=it}
    29. 29. for loopAlthough closure makes it less critical, the for statement allows to walkthrough a collection String t=’’ for(String s in ‘a’..’d’){ t+=s } assert t == ‘abcd’It is equivalent to (‘a’..’d’).each{t+=it}And for(int i in 0..<5){println i} //NB the half inclusive ..<
    30. 30. for loopAlthough closure makes it less critical, the for statement allows to walkthrough a collection String t=’’ for(String s in ‘a’..’d’){ t+=s } assert t == ‘abcd’It is equivalent to (‘a’..’d’).each{t+=it}And for(int i in 0..<5){println i} //NB the half inclusive ..<is equivalent to 5.times{println it}
    31. 31. Regular Expression• You want to know if a string match a structure? find the matches? replace the matches? Regular expressions are the answer.
    32. 32. Regular Expression• You want to know if a string match a structure? find the matches? replace the matches? Regular expressions are the answer.• Regular expression is a language by itself. Most of the features are cross languages (perl, java, php...).
    33. 33. Regular Expression• You want to know if a string match a structure? find the matches? replace the matches? Regular expressions are the answer.• Regular expression is a language by itself. Most of the features are cross languages (perl, java, php...).• TIMTOWTDI : regexp is a domain where syntax can become particularly ugly, when inappropriate choice are made.
    34. 34. Regular Expression• You want to know if a string match a structure? find the matches? replace the matches? Regular expressions are the answer.• Regular expression is a language by itself. Most of the features are cross languages (perl, java, php...).• TIMTOWTDI : regexp is a domain where syntax can become particularly ugly, when inappropriate choice are made.• A large toolkit is available, and we’ll try to present here the most commonly used and groovy oriented syntax.
    35. 35. Regular Expression• You want to know if a string match a structure? find the matches? replace the matches? Regular expressions are the answer.• Regular expression is a language by itself. Most of the features are cross languages (perl, java, php...).• TIMTOWTDI : regexp is a domain where syntax can become particularly ugly, when inappropriate choice are made.• A large toolkit is available, and we’ll try to present here the most commonly used and groovy oriented syntax.• “Mastering Regular Expressions” by Jeffrey E.L Friedl (OReilly).
    36. 36. Regular Expression• You want to know if a string match a structure? find the matches? replace the matches? Regular expressions are the answer.• Regular expression is a language by itself. Most of the features are cross languages (perl, java, php...).• TIMTOWTDI : regexp is a domain where syntax can become particularly ugly, when inappropriate choice are made.• A large toolkit is available, and we’ll try to present here the most commonly used and groovy oriented syntax.• “Mastering Regular Expressions” by Jeffrey E.L Friedl (OReilly).• http://www.regexp.info/
    37. 37. regexp: find or matchTwo operators are commonly used
    38. 38. regexp: find or matchTwo operators are commonly usedThe find operator: is a substring matching the pattern? assert “The Lord of the Rings” =~ /Lord/
    39. 39. regexp: find or matchTwo operators are commonly usedThe find operator: is a substring matching the pattern? assert “The Lord of the Rings” =~ /Lord/The match operator: is the whole string matching the pattern? assert ! “The Lord of the Rings” ==~ /Lord/ assert “The Lord of the Rings” ==~ /.*Lord.*/
    40. 40. regexp: anchoring
    41. 41. regexp: anchoringWhen finding a pattern, we can anchor it
    42. 42. regexp: anchoringWhen finding a pattern, we can anchor itTo the start of the line ^ assert ‘The Lord of the Rings’ =~ /^The Lord/ assert ! ‘The Lord of the Rings’ =~ /^Lord/
    43. 43. regexp: anchoringWhen finding a pattern, we can anchor itTo the start of the line ^ assert ‘The Lord of the Rings’ =~ /^The Lord/ assert ! ‘The Lord of the Rings’ =~ /^Lord/To the end of the line $ assert ‘The Lord of the Rings’ =~ /the Rings$/ assert ! ‘The Lord of the Rings’ =~ /Lord$/
    44. 44. regexp: anchoringWhen finding a pattern, we can anchor itTo the start of the line ^ assert ‘The Lord of the Rings’ =~ /^The Lord/ assert ! ‘The Lord of the Rings’ =~ /^Lord/To the end of the line $ assert ‘The Lord of the Rings’ =~ /the Rings$/ assert ! ‘The Lord of the Rings’ =~ /Lord$/A word boundary b assert ‘The Lord of the Rings’ =~ /bLor/ assert ‘The Lord of the Rings’ =~ /bThe Lor/ assert ‘The Lord of the Rings’ =~ /ordb/
    45. 45. regexp: character classesassert ‘The Lord’ =~ /L.rd/ // . matches any characterassert ‘The Lord’ =~ /L..d/assert ! ‘The Lord’ =~ /.The/
    46. 46. regexp: character classesassert ‘The Lord’ =~ /L.rd/ // . matches any characterassert ‘The Lord’ =~ /L..d/assert ! ‘The Lord’ =~ /.The/assert ‘The Lord’ =~ /L[eoi]rd/ //character setassert ‘The Lord’ =~ /L[A-Za-z0-9]rd/assert ’12:23:56’ =~ /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/
    47. 47. regexp: character classesassert ‘The Lord’ =~ /L.rd/ // . matches any characterassert ‘The Lord’ =~ /L..d/assert ! ‘The Lord’ =~ /.The/assert ‘The Lord’ =~ /L[eoi]rd/ //character setassert ‘The Lord’ =~ /L[A-Za-z0-9]rd/assert ’12:23:56’ =~ /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/assert ‘12_;45’ ==~ /[^a-z]*/ //anything but [a-z]
    48. 48. regexp: character classesassert ‘The Lord’ =~ /L.rd/ // . matches any characterassert ‘The Lord’ =~ /L..d/assert ! ‘The Lord’ =~ /.The/assert ‘The Lord’ =~ /L[eoi]rd/ //character setassert ‘The Lord’ =~ /L[A-Za-z0-9]rd/assert ’12:23:56’ =~ /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/assert ‘12_;45’ ==~ /[^a-z]*/ //anything but [a-z]assert ‘1234’ ==~ /d+/ //any digitassert ! ‘1234’ =~ /D/ //anything but digitassert ‘The_12_ring’ ==~ /w/ //word char [a-zA-Z0-9_]assert ! ‘The_12_ring’ =~ /W/ //non-word charassert ‘of the Rings =~ /ofsth //space char [ trnf]
    49. 49. regexp: repetitions
    50. 50. regexp: repetitions? : 0 or 1 matches ‘‘ ==~ /w?/ ‘a‘ ==~ /w?/ ‘aa’ ! ==~ /w?/
    51. 51. regexp: repetitions? : 0 or 1 matches ‘‘ ==~ /w?/ ‘a‘ ==~ /w?/ ‘aa’ ! ==~ /w?/* : zero or more ‘‘ ==~ /w*/ ‘a‘ ==~ /w*/ ‘aa‘ ==~ /w*/
    52. 52. regexp: repetitions? : 0 or 1 matches ‘‘ ==~ /w?/ ‘a‘ ==~ /w?/ ‘aa’ ! ==~ /w?/* : zero or more ‘‘ ==~ /w*/ ‘a‘ ==~ /w*/ ‘aa‘ ==~ /w*/+ : one or more ‘‘ ! ==~ /w+/ ‘a‘ ==~ /w+/ ‘aa‘ ==~ /w+/
    53. 53. regexp: repetitions (cont’d)
    54. 54. regexp: repetitions (cont’d){n} : n times assert ‘abcd’ ==~ /w{4}/
    55. 55. regexp: repetitions (cont’d){n} : n times assert ‘abcd’ ==~ /w{4}/{n,m} : between n and m times assert ‘abcd’ ==~ /w{3,6}/
    56. 56. regexp: repetitions (cont’d){n} : n times assert ‘abcd’ ==~ /w{4}/{n,m} : between n and m times assert ‘abcd’ ==~ /w{3,6}/{n,} : ≥ n times assert ‘abc’ ==~ /w{3,}/
    57. 57. regexp: repetitions (cont’d){n} : n times assert ‘abcd’ ==~ /w{4}/{n,m} : between n and m times assert ‘abcd’ ==~ /w{3,6}/{n,} : ≥ n times assert ‘abc’ ==~ /w{3,}/Greedy or not greedy ? Repetitions by default tries to consume the mostcharacters possible. Adding a question mark ‘?’ put the expanding operatorin restrictive mode ‘the Lord of the Rings’ =~ /.*s/ ‘the Lord of the Rings’ =~ /.*?s/
    58. 58. regexp: matching this or that
    59. 59. regexp: matching this or thatWe can match “multiple choices” assert ‘this’ ==~ /this|that/ assert ‘this’ ==~ /th(is|at)/
    60. 60. regexp: matching this or thatWe can match “multiple choices” assert ‘this’ ==~ /this|that/ assert ‘this’ ==~ /th(is|at)/Leftmost match: the order of the choice will infer the match ‘"So youre going to go through with it, then", Gandalf the Wizard said slowly’ =~ /going|go/ ‘"So youre going to go through with it, then", Gandalf the Wizard said slowly’ =~ /go|going/
    61. 61. regexp: modifiersIt is possible to modify the behavior of the matching engine
    62. 62. regexp: modifiersIt is possible to modify the behavior of the matching engineCase sensitivity: by default, regular expression are case sensitive assert ! ‘The Lord of the Rings’ =~ /ring/ assert ‘The Lord of the Rings’ =~ /(?i)ring/
    63. 63. regexp: modifiersIt is possible to modify the behavior of the matching engineCase sensitivity: by default, regular expression are case sensitive assert ! ‘The Lord of the Rings’ =~ /ring/ assert ‘The Lord of the Rings’ =~ /(?i)ring/Multi or single line: by default, regular expression are not evaluated acrossnew line char (n are not covered by s or .)
    64. 64. regexp: modifiersIt is possible to modify the behavior of the matching engineCase sensitivity: by default, regular expression are case sensitive assert ! ‘The Lord of the Rings’ =~ /ring/ assert ‘The Lord of the Rings’ =~ /(?i)ring/Multi or single line: by default, regular expression are not evaluated acrossnew line char (n are not covered by s or .)In other word, with single line modifier, n is considered just like anothercharacter ‘’’The Lord //NB ‘’’ for multi line string of the Rings’’’ =~ /.+/ ‘’’The Lord of the Rings’’’ =~ /(?s).+/
    65. 65. regexp: looking aroundRegular expression usually consume character
    66. 66. regexp: looking aroundRegular expression usually consume characterLet’s consider cleaving a protein sequence with trypsin ‘ACDEFKPGHRILKM’ =~/(w+?[KR])[^P]/ // the I is consumed
    67. 67. regexp: looking aroundRegular expression usually consume characterLet’s consider cleaving a protein sequence with trypsin ‘ACDEFKPGHRILKM’ =~/(w+?[KR])[^P]/ // the I is consumedIf the next amino acid is consumed, it will not be caught in a situation wherewe want to capture all the matches of the regular expression
    68. 68. regexp: looking aroundRegular expression usually consume characterLet’s consider cleaving a protein sequence with trypsin ‘ACDEFKPGHRILKM’ =~/(w+?[KR])[^P]/ // the I is consumedIf the next amino acid is consumed, it will not be caught in a situation wherewe want to capture all the matches of the regular expressionHopefully, we can ‘lookahead’ ‘ACDEFKPGHRILKM’ =~/(w+?[KR])(?=[^P])/
    69. 69. regexp: looking aroundRegular expression usually consume characterLet’s consider cleaving a protein sequence with trypsin ‘ACDEFKPGHRILKM’ =~/(w+?[KR])[^P]/ // the I is consumedIf the next amino acid is consumed, it will not be caught in a situation wherewe want to capture all the matches of the regular expressionHopefully, we can ‘lookahead’ ‘ACDEFKPGHRILKM’ =~/(w+?[KR])(?=[^P])/Respectively, it is possible to check is a regexp matches before the capturedstring (look behind) (?<=...)
    70. 70. regexp at works : splitting a stringOne of the most commons use of the regular expression is to split a stringinto an array
    71. 71. regexp at works : splitting a stringOne of the most commons use of the regular expression is to split a stringinto an arraySplitting point can be defined by a regular expression assert “a b c d”.split(/s+/)==[‘a’, ‘b’, ‘c’, ‘d’]
    72. 72. regexp at works : splitting a stringOne of the most commons use of the regular expression is to split a stringinto an arraySplitting point can be defined by a regular expression assert “a b c d”.split(/s+/)==[‘a’, ‘b’, ‘c’, ‘d’]An extra argument can be passed, to limit the number of elements in thereturned list assert “a b c d”.split(/s+/, 3)==[‘a’, ‘b’, ‘c d’]
    73. 73. regexp at works : splitting a stringOne of the most commons use of the regular expression is to split a stringinto an arraySplitting point can be defined by a regular expression assert “a b c d”.split(/s+/)==[‘a’, ‘b’, ‘c’, ‘d’]An extra argument can be passed, to limit the number of elements in thereturned list assert “a b c d”.split(/s+/, 3)==[‘a’, ‘b’, ‘c d’]Do not forget to pipe to an collect closure assert “0 1 2”.split(/s+/).collect{it as Integer}
    74. 74. regexp at works : splitting a stringOne of the most commons use of the regular expression is to split a stringinto an arraySplitting point can be defined by a regular expression assert “a b c d”.split(/s+/)==[‘a’, ‘b’, ‘c’, ‘d’]An extra argument can be passed, to limit the number of elements in thereturned list assert “a b c d”.split(/s+/, 3)==[‘a’, ‘b’, ‘c d’]Do not forget to pipe to an collect closure assert “0 1 2”.split(/s+/).collect{it as Integer}Or use splitEachLine txt.splitEachLine(/regexp/){it.action}
    75. 75. regexp: loop across matchesAll the occurrence of a match is returned into a list: String title = "The Lord of the Rings" assert (title=~/(?i)thes+w+/).collect{"<$it>"} == [‘<The Lord>’, ‘<the Rings>’]
    76. 76. regexp: loop across matchesAll the occurrence of a match is returned into a list: String title = "The Lord of the Rings" assert (title=~/(?i)thes+w+/).collect{"<$it>"} == [‘<The Lord>’, ‘<the Rings>’]It is also natural to loop across all such occurrences: (title=~/(?i)thes+w+/).each{println "<$it>"}
    77. 77. regexp: loop across matchesAll the occurrence of a match is returned into a list: String title = "The Lord of the Rings" assert (title=~/(?i)thes+w+/).collect{"<$it>"} == [‘<The Lord>’, ‘<the Rings>’]It is also natural to loop across all such occurrences: (title=~/(?i)thes+w+/).each{println "<$it>"}Is equivalent to title.eachMatch(/(?i)thes+w+/){println "<$it>"}
    78. 78. regexp: capturingLike in perl, it is possible to define with parenthesis sub patterns to becaptured in the pattern
    79. 79. regexp: capturingLike in perl, it is possible to define with parenthesis sub patterns to becaptured in the patternIf such parenthesis are present, the java.util.regex.Matcher structurereturned is different def matcher = title=~/(?i)thes+(w+)/ assert matcher.hasGroup() assert matcher[0][0] == ‘The Lord’ //the whole match assert matcher[0][1] == ‘Lord‘ //first captured block
    80. 80. regexp: capturingLike in perl, it is possible to define with parenthesis sub patterns to becaptured in the patternIf such parenthesis are present, the java.util.regex.Matcher structurereturned is different def matcher = title=~/(?i)thes+(w+)/ assert matcher.hasGroup() assert matcher[0][0] == ‘The Lord’ //the whole match assert matcher[0][1] == ‘Lord‘ //first captured blockIt is possible to loop around with a closure (title=~/(?i)thes+(w+)/).each{all, name -> println "all: <$all>, capt:<$name>" }

    ×