[2019 oracle code one] mastering regular expressions

Mastering Regular Expressions (^.*$)(?#everything) You Should Know

Speakers: Fernando Babadopulos @babadopulos

For more blog posts, see The Oracle Code One table of contents


History

  • Invented in 1951
  • Popularized in 70’s with vi/lex/sed/awk/expr
  • In 80’s Perl included advanced regex
  • Java 1.4 – added regex

General

  • Used for a spam filter before AI started detecting spam
  • Not just for developers. Can do lots of things in text editor

Engine

  • Eager – starts with leftmost match and first match is good enough.

Regex Examples

  • /Java/ – match first string “Java”
  • /database/g/ – match all instances of string “database” (different syntax than for Java. Typing without the / from now one)
  • .* – match everything (or nothing) – as much as can because eager
  • [dl]ate – character class matching d or l (followed by ate)
  • a[^p] – negated character class. No “p” after “a”
  • database[0-9]\. – range of database0. to database9.
  • \d\d.\d\d – Two digits, any character and two more digits. Probably not what you want. Works if have valid data. But will also match 15624. Use what want ex: [:h] instead of dot. (Or escape the dot if want a period)
  • ^(.*),(.*) – Want first two fields of a CSV. [unless have commas inside field with quotes]. Backtracks a lot though because consumes entire string and backtracks one character at a time until gets to a comma. Then backtracks more. Better to write [^,] than dot so express what actually want.
  • ^([^,]*),[^,]*)$ – match exactly two field [Unless have commas within quotes for an element]
  • ^[a-z]*$ – empty file or only lower case letters
  • get|set – match either string
  • \bget|set\b – matches words that end with get or start with set – not what intended
  • \b(get|set)\b – only match words get or set since checking for boundary (space, tab, etc)
  • Jan(uary)? – matches Jan or January
  • a{0,10} – 0-10 a’s
  • a{10} – exactly 10 a’s
  • a{0,} – zero or more a’s
  • a{1,} – at least one a’s
  • Java(?=Script) – positive lookahead – Java followed by Script
  • Java(?!Script) – negative lookahead – Java not followed by Script

Escaping

  • \( -escape paren
  • \d – shorthand for digit
  • \s – shorthand for whitespace
  • \w – shorthand for word
  • \\ – slash

Tips

  • Avoid . – use characters want or negative character class.
  • Use anchors (^ and $) wherever possible.

Regex101.com

  • Type regex
  • Gives explanation of regex typed
  • Can set flags ex: global
  • Area for test string to see what matches
  • Has a code generator so can get regex with proper Java escaping
  • Has debugger – can step through the parts of the reg ex and see what matches at each step. It also shows backtracking. This seems like a good way to see the efficiency of a regex as well. [Cool!]

Other URLs:

https://www.regular-expressions.info – tutorial

https://regexper.com – create graphs

My take

I enjoyed the debate (and then vote) on how to pronounce regex before the talk started! Half of the audience raised their hands for liking regular expressions. Biased crowd of course. The room was awkward and the lecturn hid part of the screen. I like that he showed a lot of examples and the execution graph. I really like the debugger on regex101. Learning that was worth attending the talk on its own! As was the regexper graph site

JavaOne – Simplified and Fast Fraud Detection

Simplified and Fast Fraud Detection”

Speaker: Keith Laker

For more blog posts from JavaOne, see the table of contents


Live SQL

  • free online Oracle 12C database
  • Can save scripts
  • Google searchable
  • Each OTN (oracle tech network) users sees own copy of data. Sandboxed
  • Can download data as CSV

https://livesql.oracle.com/apex/livesql/file/index.html

And for this session the live sql URL

Pattern Matching

  • types – regex, sed/awk
  • in SQL – row level regex
  • new: pattern recognition in a stream or rows – aka can match across rows and columns
  • new SQL construct MATCH_RECOGNIZE – ANSII standard; not Oracle specific

Steps

  1. Bucket and order the data
    • This makes the patterns “visible”.
    • Used order by or partition by/order by so queries are deterministic (this does not require the paid Oracle partitioning feature)
  2. Define the pattern
    • Regular expression like pattern
    • Ex: PATTERN (X+ Y+ Z+) where X/Y/Z is a boolean expression. Ex: bal < PREV(bal)
    • Common qualifiers: * + ? {n} {n,} {n,m}
    • Also have extra ? for reluctant qualifiers – helps deal with what to do with overlapping matches
  3. Define measures
    • Define columns in output table
    • pattern navigation options; PREV, NEXT, FIRST, LAST
    • column
    • optional aggregates (COUNT, SUM, AVG, MAX, MIN)
    • special measures: CLASSIFIER() – which component of the pattern applied to this row and MATCH_NUMBER() – how many matches within each partition – both are good for debugging
    • Ex: MEASURES FIRST(x.tstamp) as first_x
  4. Controlling output
    • by default get a column per measure along with the partitioning column (when using one row per match). Get more columns with all rows per match)
    • how many rows back: ONE ROW PER MATCH (default) ALL ROWS PER MATCH or ALL ROWS PER MATCH WITH UNMATCHED ROWS (good for debugging)
    • where to start next search: AFTER MATCH SKIP PAST LAST ROW (default), also options for next row and relating to variables

Demo

  • Find 3 or more small (<2K) money transfers within 30 days. Then find large transfer (?=1M) within 10 days of last small transfer
  • Can do in SQL without pattern matching, but a lot of code.
  • Can do in Java, but. [copying the database…]
  • Showed how to create a table for JSON data – reads into a CLOB and Oracle checks it is valid JSON. Loaded with insert statements because live sql is web based and can’t access underlying file system.
  • Can use dot notation to access SQL fields

Sample pattern matching statement:


SELECT *
FROM transfers_view
MATCH_RECOGNIZE(
 ORDER BY time_id
 MEASURES
 user_id AS user_id,
 amount AS amount
 PATTERN (X{3,} Y)
 DEFINE
 X AS (amount < 2000) AND 
 LAST(time_id) - FIRST(time_id) < 30,
 Y AS (amount >= 1000000) AND 
 time_id - LAST(x.time_id)< 10);

My take: This was a two hour “tutorial” which differs from a hands on lab. We were still able to follow along with a laptop or “large tablet.” I followed along with the demos on my Mac. Which also let me play a bit. It was fun. I’ve always liked SQL :). I like that he uses QR codes for the links/blogs he wants people to go to. They are also linked in the PowerPoint when it becomes available.

It was also interesting blogging on my laptop. On my tablet, I blog in HTML because it is a pain to u se the visual editor on the tablet. A laptop has no such problem. But a laptop battery doesn’t last all day so…

The 8 Nights of Java – Night 4

Continuing The 8 Nights of Java series, tonight we focus on one of the single most important releases of Java. Java 1.4 was released a time when many businesses were starting to look to Java as a foundation for their software systems. After years of licensing proprietary or difficult to use software, Java was seen as a breadth of fresh air for many software engineers. It was helped, in part, by the decline of Windows-based computers and explosive growth of Mac and open-source Linux systems in the workplace and in homes. After all, if all of your developers are using different operating systems, then you need a software development platform that works on all of them and in that, Java was a success. So many business adopted Java 1.4 during this time and stayed on Java 1.4 for over a decade. In fact, many large enterprise systems still rely on Java 1.4 to this day. Hopefully, someone will be hired to update them soon!

Jump to: [Night 1 | Night 2 | Night 3 | Night 4 | Night 5 | Night 6 | Night 7 | Night 8]

Java 1.4 Notable Features
Sun released Java 1.4 (codename Merlin) on February 6th, 2002. Key new functionality included:

  • Regular expressions
  • Assertions
  • NIO Version 1
  • XML/XSLT support

From Jeanne:

I love regular expressions. They are one of my favorite language features because they are concise and expressive when written well. I was excited when they came out. While I started programming as a full time job after Java 1.4 was released, we were still using 1.3 as we waited for the application servers to support Java 1.4. This meant I was already employed and got to teach my teammates about regular expressions. I’ve actually given that presentation a number of times since.

I don’t use assertions because I write a lot of unit tests and the unit tests tell me about the type of problem that an assertion would. Tests help me design my code in a way that I don’t need assertions to tell me about the state of affairs. And then there is poor New I/O. I really like Java 7 NIO.2. New I/O “1”, not so much. It served it’s purpose in getting us to NIO.2 though.

From Scott:

I started programming professionally around the time that XML/XSLT were seen as the “new hot technology” to use on build enterprise systems. Having built-in support for XML transformations made Java look cutting edge at the time. While a lot of what is now done with XML is instead done by JSON, XML is still the core of many data-based systems. In fact, numerous web and mobile frond-end languages still use XML for their layouts, even if the developers using them rely solely on a GUI-based editor. Either way, Java 1.4 demonstrated that new technologies could be integrated into the JVM quite rapidly. That said, I’m still waiting for a JSON parser to become part of the standard Java runtime environment!

Java 1.4 also introduced NIO version 1, or NIO.1 for short. While NIO.2 is a quite powerful, if not commonly used framework, NIO.1 is basically dead weight at this point. The NIO.1 API never really caught on and today, very few people rely on file channels and the like. Since a key part of Java is keeping backwards compatibility, it remains part of the JRE, albeit rarely used.