Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

Wednesday, March 28, 2012

JRegex

@Grapes(
    @Grab(group='net.sourceforge.jregex', module='jregex', version='1.2_01')
)
import jregex.*
//http://jregex.sourceforge.net/gstarted-advanced.html#imatching

String userNameChars="[\\w.\\-]+" //letters, dots, hyphens
String alphaNums="\\w+"
String dot="\\."
Pattern email=new Pattern(userNameChars + "@" + "(?:"+ alphaNums + dot + ")*" + alphaNums)

List ls = ['s@1.com','2@3.com','howdy@d.']

ls.each {
    println email.matcher(it).matches()
}

Pattern re=new Pattern("\\d\\d:\\d\\d") //two digits + colon + two digits
println("Pattern: "+re)

Matcher m=re.matcher()
test(m,"") // true
test(m,"1") // true
test(m,"12") // true
test(m,"1:2") // true
test(m,"12:") // true
test(m,"12:1") // true
test(m,"12:12") // true
test(m,"12:123") // true
test(m,"1:") // false
test(m,"123:1") // false

void test(Matcher m1, String s) {
   m1.setTarget(s)
   println("\""+s+"\" : "+m1.matchesPrefix()) //note: .matchesPrefix()
}

////*Pure Java/Groovy*////
//credit: http://glaforge.appspot.com/article/incomplete-string-regex-matching
List inp = [
    /12:/,/12:1/,/1/,/12:12/ //trues
    ,/1:/,/123:1/ //falses
]

inp.each {
    def matcher = it =~ /\d{2}:\d{2}/
    println matcher.matches() || matcher.hitEnd()
}

println 'done'

Monday, February 27, 2012

Regex look-ahead/behind syntax

//http://groovy.codehaus.org/Regular+Expressions
def r = /e+/
def str = 'me cheese please'
def m = str =~ r

assert m.size() == 5
println m*.toString() //[e, ee, e, e, e]

/////////////////////////////

def r = /(This boy) is/
def str = 'This boy is 10. This boy wants chocolate. This boy is tall.'
def m = str =~ r

assert m.size() == 2
assert m.collect { it[1] } == ['This boy','This boy']

/////////////////////////////

assert "abc".replaceAll(/(a)(b)(c)/, "\$1\$3") == 'ac' //back references

/////////////////////////////

//http://www.regular-expressions.info/captureall.html
r = /((?:abc|123)+).*?/
str = '123abc 123abc123'
m = str =~ r

assert m.size() == 2
println m*.toString()
print m[0][1..-1]
println m[1][1..-1]

/////////////////////////////

//password of 8 characters long and two non-letters
def r1 = /.*[^a-zA-Z].*[^a-zA-Z].*(?<=.{7})/  //look-behind ?<=

assert 'abc' !=~ r1
assert 'abcde12' !=~ r1
assert 'abcdef12' ==~ r1
assert 'abc1defgggg2' ==~ r1
assert 'abc1defgggg' !=~ r1

//word is foo$wrd*, store $wrd*
def wrd = 'bar'
def r2 = /foo((?=$wrd)[\w]+)/  //look-ahead ?=

def foo = "foo${wrd}hellow"
assert foo ==~ r2
def m = foo =~ r2
assert m[0][1] == "${wrd}hellow" // note: $wrd not consumed by check, is stored in result

foo = 'foohellow' // no $wrd
assert foo !=~ r2

//ADVANCED/////////////////////////////
def churnText(String text) {
    def points = [[k: ~/(N|n)orth(E|e)ast(ern)?/                                                                , v:'NE'],
              [k: ~/(?>(N|n)orth(W|w)est(ern|:)?)(?! Territories)/                                              , v:'NW'],
              [k: ~/(S|s)outheast(ern)?/                                                                        , v:'SE'],
              [k: ~/(?>(S|s)outh(\s)?(W|w)est(ern)?)(?! Hill| Bend)/                                            , v:'SW'],
              [k: ~/(?>(N|n)orth(ern)?|Upstate)(?! Carolina| Dakota| Platte| Neck| Mariana Islands| Bay|ridge)/ , v:'N' ],
              [k: ~/(E|e)ast(ern)?/                                                                             , v:'E' ],
              [k: ~/(?>(S|s)outh(ern|side)?)(?! Carolina| Dakota)/                                              , v:'S' ],
              [k: ~/(?!(?<=George ))(?>(W|w)est(ern| of the)?)(?! Virginia| Palm Beach)/                        , v:'W' ],
              [k: ~/(?>(C|c)entral|Center|Middle|the middle section of the)(?!town| Peninsula| Tennessee|ia)/   , v:'C' ]]
    
    points.each {p ->
      def matcher = (text =~ p.k)
      text = matcher.replaceAll(p.v)
      println "p.v: ${p.v} text: $text"
    }
}

churnText('Northwest Virginia')
println '='*40
churnText('NorthWestern Territories')
println '='*40
churnText('East George West')
return

Monday, May 16, 2011

regex pattern "matches" ==~ with metaclass

Closure foundIn = { a, b ->
  print (/$a --> ${b}?? /); 
  return (a ==~ /${b}/); 
}
 
String testConditionRegex = "[abc]"

println foundIn('a', testConditionRegex) //true
println foundIn('z', testConditionRegex) //false
println foundIn(1, testConditionRegex) //false
 
println foundIn(2, 2) //true
println foundIn(12, 12.0) //false
println foundIn(null, testConditionRegex) //false
 
Object.metaClass.foundIn = { return foundIn (delegate, it); }
 
println ("4".foundIn("[1-9]")) //true
println (8.foundIn("[1-9]")) //true
println ((-8).foundIn("[1-9]")) //false

see: http://groovyconsole.appspot.com/script/485002