Regex CLASS

The regular expression.

Julian implements a core regex in a similar syntax as Perl and JavaScript, but intentionally doesn't build extension features into it, such as various named patterns, occurrence count, non-greedy matching, etc. If advanced feature is required, the user can use type mapping API to call directly to regex facilities provided by the platform.

The supported constructs include:

  • uniona|b
  • concatenationabc
  • quantifiers? * + (representing zero-or-one, zero-or-more and one-or-more, respectively)
  • range[a-zA-Z]
  • negated range[^0-9]
Additionally, these characters have special meaning:
  • . matches every character
  • ^ matches the start of input
  • $ matches the end of input
One can also use () to capture the groups. Groups can be retrieved by Match.getGroup(int).

   Regex r = new Regex("(a?(b*)c?)"); // There will be two groups in the matching result, if matched.

Regex has language-level support in Julian. To create a regex literal, enclose the pattern with '/'. Such literals are more clean to use in comparison to calling the constructor, especially when escaping multiple metacharacters in the pattern.

   Regex r = /ab*d?e/;

The metacharacters are: . | - ( ) [ ] ? * + \. If matching a character which happens to be the metacharacter, use \ to escape:

   Regex r = /a\.b/; // Matches exactly "a.b", not "a{any character}b"

The regex API can be used in one of two ways. The first is to match the pattern against the entire input with matchAll(string), which returns a Match object that contains the matching info, but effectively cannot move forward anymore, as the entire input has been matched.

   Match m = /ab+c/.matchAll("abbbbc");
   bool result = m.isMatched(); // true
   m.next(); // since we have matched the entire string, this will only result in a mismatch
   result = m.isMatched(); // false


The alternative is to match by matchNext(string), which will try to match the first occurrence of the pattern in the input. The Match object it returns contains the matching info for the previous match, and the user may call next() to find the next match, until the entire input is through. In this approach, the Match object remains stateful and updates itself every time next() is called before the entire input has been matched.

   Match m = /ab+c/.matchNext("abbbbcacabc");
   while (m.isMatched()) {
     string text = m.getText(); // Get the matched text;
     Console.println(text);
     m.next(); // Match the next occurrence pattern "ab+c"
   }

   // (output)
   // abbbbc
   // abc

Parent Class

All Members


TypeNameSignature
constructorRegexpublic Regex(string)
methodmatchAllpublic Match matchAll(string)
methodmatchNextpublic Match matchNext(string)

Constructors


public Regex(string pattern)

Create a new Regex object with the given pattern.

Parameters

  • pattern The regex pattern.

Throws


Methods


public Match matchAll(string input)

Match the entire input against this pattern.

Parameters

  • input The input to match in its entirety.

Returns

  • A Match object that contains the matching information.

public Match matchNext(string input)

Find the first occurrence of the pattern in the given input.

Parameters

  • input The input to match from the beginning.

Returns

  • A Match object that contains the matching information. If it's a match, one may call next() to get the next occurrence.