code: def fromregex(file, regexp, dtype, encoding=None):
      """
      Construct an array from a text file, using regular expression parsing.
  
      The returned array is always a structured array, and is constructed from
      all matches of the regular expression in the file. Groups in the regular
      expression are converted to fields of the structured array.
  
      Parameters
      ----------
      file : str or file
          File name or file object to read.
      regexp : str or regexp
          Regular expression used to parse the file.
          Groups in the regular expression correspond to fields in the dtype.
      dtype : dtype or list of dtypes
          Dtype for the structured array.
      encoding : str, optional
          Encoding used to decode the inputfile. Does not apply to input streams.
  
          .. versionadded:: 1.14.0
  
      Returns
      -------
      output : ndarray
          The output array, containing the part of the content of `file` that
          was matched by `regexp`. `output` is always a structured array.
  
      Raises
      ------
      TypeError
          When `dtype` is not a valid dtype for a structured array.
  
      See Also
      --------
      fromstring, loadtxt
  
      Notes
      -----
      Dtypes for structured arrays can be specified in several forms, but all
      forms specify at least the data type and field name. For details see
      `doc.structured_arrays`.
  
      Examples
      --------
      >>> f = open('test.dat', 'w')
      >>> f.write("1312 foo\\n1534  bar\\n444   qux")
      >>> f.close()
  
      >>> regexp = r"(\\d+)\\s+(...)"  # match [digits, whitespace, anything]
      >>> output = np.fromregex('test.dat', regexp,
      ...                       [('num', np.int64), ('key', 'S3')])
      >>> output
      array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')],
            dtype=[('num', '<i8'), ('key', '|S3')])
      >>> output['num']
      array([1312, 1534,  444], dtype=int64)
  
      """
      own_fh = False
      if not hasattr(file, "read"):
          file = np.lib._datasource.open(file, "rt", encoding=encoding)
          own_fh = True
  
      try:
          if not isinstance(dtype, np.dtype):
              dtype = np.dtype(dtype)
  
          content = file.read()
          if isinstance(content, bytes) and isinstance(
              regexp, np.unicode
          ):
              regexp = asbytes(regexp)
          elif isinstance(content, np.unicode) and isinstance(
              regexp, bytes
          ):
              regexp = asstr(regexp)
  
          if not hasattr(regexp, "match"):
              regexp = re.compile(regexp)
          seq = regexp.findall(content)
          if seq and not isinstance(seq[0], tuple):
              # Only one group is in the regexp.
              # Create the new array as a single data-type and then
              #   re-interpret as a single-field structured array.
              newdtype = np.dtype(dtype[dtype.names[0]])
              output = np.array(seq, dtype=newdtype)
              output.dtype = dtype
          else:
              output = np.array(seq, dtype=dtype)
  
          return output
      finally:
          if own_fh:
              file.close()
  
imports: from __future__ import division, absolute_import, print_function
  import sys
  import os
  import re
  import functools
  import itertools
  import warnings
  import weakref
  from operator import itemgetter, index
  import numpy
  from .None import format
  from ._datasource import DataSource
  from numpy.core import overrides
  from numpy.core.multiarray import packbits, unpackbits
  from numpy.core.overrides import set_module
  from numpy.core._internal import recursive
  from ._iotools import LineSplitter, NameValidator, StringConverter, ConverterError, ConverterLockError, ConversionWarning, _is_string_like, has_nested_fields, flatten_dtype, easy_dtype, _decode_line
  from numpy.compat import asbytes, asstr, asunicode, asbytes_nested, bytes, basestring, unicode, os_fspath, os_PathLike
  from numpy.core.numeric import pickle
label_desc: Use image node_id 15 for calling a global function with example usage: fromregex(file, regexp, dtype, encoding) and returns: output