Dart RegExp hashCode
Syntax & Examples

RegExp.hashCode property

The `hashCode` property in Dart's `RegExp` class returns the hash code for an object.


Syntax of RegExp.hashCode

The syntax of RegExp.hashCode property is:

 int hashCode 

This hashCode property of RegExp the hash code for this object.

Return Type

RegExp.hashCode returns value of type int.



✐ Examples

1 Hash code for a specific word pattern

In this example,

  1. We create a `RegExp` object named `pattern` with the pattern 'hello'.
  2. We retrieve its hash code using the `hashCode` property.
  3. We then print the hash code to standard output.

Dart Program

void main() {
  RegExp pattern = RegExp('hello');
  int hash = pattern.hashCode;
  print('Hash code: $hash');
}

Output

Hash code: 451537081

2 Hash code for a digit pattern

In this example,

  1. We create a `RegExp` object named `pattern` with the pattern '[0-9]+' to match one or more digits.
  2. We retrieve its hash code using the `hashCode` property.
  3. We then print the hash code to standard output.

Dart Program

void main() {
  RegExp pattern = RegExp('[0-9]+');
  int hash = pattern.hashCode;
  print('Hash code: $hash');
}

Output

Hash code: 512605508

3 Hash code for a word character pattern

In this example,

  1. We create a `RegExp` object named `pattern` with the raw pattern '\w+' to match one or more word characters.
  2. We retrieve its hash code using the `hashCode` property.
  3. We then print the hash code to standard output.

Dart Program

void main() {
  RegExp pattern = RegExp(r'\w+');
  int hash = pattern.hashCode;
  print('Hash code: $hash');
}

Output

Hash code: 509502384

Summary

In this Dart tutorial, we learned about hashCode property of RegExp: the syntax and few working examples with output and detailed explanation for each example.