公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.String.match
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
根据正则表达式匹配字符串。返回匹配的字符串列表。
用法 | 返回 |
---|
String.match(regex, flags) | 列表 |
参数 | 类型 | 详细信息 |
---|
此:input | 字符串 | 要搜索的字符串。 |
regex | 字符串 | 要匹配的正则表达式。 |
flags | 字符串,默认值:"" | 一个字符串,用于指定正则表达式标志的组合,具体为一个或多个:'g'(全局匹配)或 'i'(忽略大小写)。 |
示例
代码编辑器 (JavaScript)
var s = ee.String('ABCabc123');
print(s.match('')); // ""
print(s.match('ab', 'g')); // ab
print(s.match('ab', 'i')); // AB
print(s.match('AB', 'ig')); // ["AB","ab"]
print(s.match('[a-z]+[0-9]+')); // "abc123"
print(s.match('\\d{2}')); // "12"
// Use [^] to match any character except a digit.
print(s.match('abc[^0-9]', 'i')); // ["ABCa"]
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
import ee
import geemap.core as geemap
Colab (Python)
s = ee.String('ABCabc123')
print(s.match('').getInfo()) # ""
print(s.match('ab', 'g').getInfo()) # ab
print(s.match('ab', 'i').getInfo()) # AB
print(s.match('AB', 'ig').getInfo()) # ['AB','ab']
print(s.match('[a-z]+[0-9]+').getInfo()) # 'abc123'
print(s.match('\\d{2}').getInfo()) # '12'
# Use [^] to match any character except a digit.
print(s.match('abc[^0-9]', 'i').getInfo()) # ['ABCa']
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe \u003ccode\u003eString.match()\u003c/code\u003e method searches a string for a specified regular expression and returns a list of matches.\u003c/p\u003e\n"],["\u003cp\u003eIt accepts the input string, the regular expression pattern, and optional flags ('g' for global match and 'i' for case-insensitive search).\u003c/p\u003e\n"],["\u003cp\u003eIf a match is found, the method returns a list containing the matching substring(s); otherwise, it returns an empty list.\u003c/p\u003e\n"],["\u003cp\u003eYou can use various regular expression patterns and flags to control the matching behavior, including character classes, quantifiers, and anchors.\u003c/p\u003e\n"]]],["The `String.match()` function searches a string (`input`) for matches to a given regular expression (`regex`). It returns a list of matching strings. Optional flags (`flags`) modify the search, such as 'g' for global matching or 'i' for case-insensitive matching. The examples demonstrate various regex patterns and flag combinations. The function returns an empty string if the pattern is empty, or list of matching strings when successful. The examples cover the use case for Javascript and Python.\n"],null,["# ee.String.match\n\nMatches a string against a regular expression. Returns a list of matching strings.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|----------------------------------|---------|\n| String.match`(regex, `*flags*`)` | List |\n\n| Argument | Type | Details |\n|---------------|---------------------|--------------------------------------------------------------------------------------------------------------------------------------|\n| this: `input` | String | The string in which to search. |\n| `regex` | String | The regular expression to match. |\n| `flags` | String, default: \"\" | A string specifying a combination of regular expression flags, specifically one or more of: 'g' (global match) or 'i' (ignore case). |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\nvar s = ee.String('ABCabc123');\nprint(s.match('')); // \"\"\nprint(s.match('ab', 'g')); // ab\nprint(s.match('ab', 'i')); // AB\nprint(s.match('AB', 'ig')); // [\"AB\",\"ab\"]\nprint(s.match('[a-z]+[0-9]+')); // \"abc123\"\nprint(s.match('\\\\d{2}')); // \"12\"\n\n// Use [^] to match any character except a digit.\nprint(s.match('abc[^0-9]', 'i')); // [\"ABCa\"]\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\ns = ee.String('ABCabc123')\nprint(s.match('').getInfo()) # \"\"\nprint(s.match('ab', 'g').getInfo()) # ab\nprint(s.match('ab', 'i').getInfo()) # AB\nprint(s.match('AB', 'ig').getInfo()) # ['AB','ab']\nprint(s.match('[a-z]+[0-9]+').getInfo()) # 'abc123'\nprint(s.match('\\\\d{2}').getInfo()) # '12'\n\n# Use [^] to match any character except a digit.\nprint(s.match('abc[^0-9]', 'i').getInfo()) # ['ABCa']\n```"]]