working with regular expressions in Java

satya - 3/29/2013 2:10:16 PM

regex matching in Java

regex matching in Java

Search for: regex matching in Java

satya - 3/29/2013 2:12:17 PM

Here is a java regex tutorial

Here is a java regex tutorial

satya - 3/29/2013 2:13:15 PM

Here is a comprehensive site on regex including java

Here is a comprehensive site on regex including java

satya - 3/29/2013 2:13:47 PM

java.util.regex package

Search for: java.util.regex package

satya - 8/18/2020, 7:42:12 PM

regex cheetsheet

regex cheetsheet

satya - 8/18/2020, 7:42:35 PM

online regex tester

online regex tester

satya - 8/18/2020, 7:43:43 PM

Somework in powershell I did in regex

Somework in powershell I did in regex

satya - 8/18/2020, 7:54:42 PM

Use of . dot in regex

Use of . dot in regex

Search for: Use of . dot in regex

satya - 8/18/2020, 7:59:31 PM

Behavior of [] called a character class is explained here

Behavior of [] called a character class is explained here

satya - 8/18/2020, 8:02:25 PM

(.*) inside a group in regex

(.*) inside a group in regex

Search for: (.*) inside a group in regex

satya - 8/18/2020, 8:08:29 PM

Brackets are explained here

Brackets are explained here

satya - 8/18/2020, 8:15:57 PM

See this medium article

See this medium article

satya - 8/18/2020, 8:29:23 PM

Extracting a pattern


Filename: SomeString_2020-03-26_12-50-00.csv

([a-zA-Z0-9]+)_(\\d{4}-\\d\\d-\\d\\d)_(\\d\\d-\\d\\d-\\d\\d)\\.(.*)

Breaking it down

Any name that has alphanumeric one or more
([a-zA-Z0-9]+)

Separator _
_

Digits
(\\d{4}-\\d\\d-\\d\\d)
_


Digits
(\\d\\d-\\d\\d-\\d\\d)

Literal escaped "."
\\.

Any thing in extension
(.*)

satya - 8/18/2020, 8:30:51 PM

Another example


Forecaster_plant_HA_2020-02-22_10-00-00.csv

^[a-zA-Z0-9]+_[a-zA-Z0-9]+_HA_\d\d\d\d-\d\d-\d\d_\d\d-\d\d-\d\d.csv$

satya - 8/18/2020, 8:31:42 PM

Quickly


* Zero or more
+ one or more
? one or none
. any character

satya - 8/18/2020, 8:32:43 PM

Inside a character class the "." looses its value


[.x]+ means "." or "x" one or more time
you don't have to do 

[\.x\ to escape the "."

satya - 8/18/2020, 8:33:06 PM

2030 I am still finding it hard to find a decent reference for REGEX!!!!!

2030 I am still finding it hard to find a decent reference for REGEX!!!!!

satya - 8/18/2020, 10:02:33 PM

Using groups to split a string


public static List<String> splitString(String s, String regexPattern)
      {
         Pattern p = Pattern.compile(regexPattern);
         Matcher m = p.matcher(s);
         if (m.find() == false)
         {
            return null;
         }
         int count = m.groupCount();
         // index 0 is the whole match. So ignore it
         List<String> parts = new ArrayList<String>();
         for(int i=1; i<= count;i++)
         {
            parts.add(m.group(i));
         }
         return parts;
      }

satya - 8/18/2020, 10:02:41 PM

To test it


private static void test2()
   {
      String s = "SomeString_2020-03-26_12-50-00.csv";
      String regex = "([a-zA-Z0-9]+)_(\\d{4}-\\d\\d-\\d\\d)_(\\d\\d-\\d\\d-\\d\\d)\\.(.*)";
      
      List<String> fields = splitString(s, regex);
      putils.p(fields.toString());
   }

satya - 8/18/2020, 10:03:09 PM

Prints


[SomeString, 2020-03-26, 12-50-00, csv]

satya - 8/18/2020, 10:07:00 PM

Some basic features

Some basic features

satya - 8/18/2020, 10:10:52 PM

A more comprehensive reference for characters

A more comprehensive reference for characters