1.定义
* 允许0 次或多次重复
+ 允许1次或多次重复
? 允许0次或1次重复
{n,m} 允许n到m次重复
{n} 允许n次重复
^ 匹配字符串开头
$ 匹配字符串结尾
. 匹配除换行符外所有字符
2.事例
*正则表达式基本操作 tuple_regexp_match ('abba', 'ab*', Matches) *return abb tuple_regexp_match ('abbaa', 'ba*', Matches) *return b tuple_regexp_match ('abaa', 'ba*', Matches) *return baa tuple_regexp_match ('abba', 'a*b*', Result) * Returns 'abb' tuple_regexp_match ('abba', 'b*a*', Result) * Returns 'a' tuple_regexp_match ('babba', 'b*a*', Result) * Returns 'ba' tuple_regexp_match ('abba', 'b+a*', Result) * Returns 'bba' tuple_regexp_match ('abba', '.a', Result) * Returns 'ba' tuple_regexp_match ('abba', '[ab]*', Result) * Returns 'abba' tuple_regexp_match ('abba', '[ba]*', Result) * Returns 'abba' tuple_regexp_match (['img123','img124'], 'img(.*)', Result) * Returns ['123','124'] tuple_regexp_match ('mydir/img001.bmp', 'img(.*)\\.(.*)', Result) * Returns ['001','bmp'] tuple_regexp_test ('p10662599755', '[A-Z]*', Result) * Returns 0 tuple_regexp_test ('p10662599755', ['[A-Z]*','ignore_case'], Result) * Returns 1 tuple_regexp_test ('ababab', '(ab){3}', NumMatches) *return 1 tuple_regexp_test ('abab', '(ab){3}', NumMatches) *return 0 tuple_regexp_test ('abababa', '(ab){3}', NumMatches) *return 1 tuple_regexp_test ('abababa', '^(ab){3}$', NumMatches) *return 0 tuple_regexp_test ('ababab', '^(ab){3}$', NumMatches) *return 1 tuple_regexp_replace ('abba', 'b*', 'x', Result) *return xabba tuple_regexp_replace ('abba', 'a*', 'x', Result) *return xbba tuple_regexp_replace ('abba', 'b', 'x', Result) *return axba tuple_regexp_replace ('abba', ['b','replace_all'], 'x', Result) *return axxa tuple_regexp_replace(['img10.bmp','img11.bmp','img12.bmp'], 'img(.*).bmp', 'out$1.txt', Result) * Returns ['out10.txt','out11.txt','out12.txt'] tuple_regexp_replace (['SN/1234567-X','SN/2345678-Y','SN/3456789-Z'], 'SN/(\\d{7})-([A-Z])', 'Product Model $2, Serial Number $1', Result) *return ['Product Model X, Serial Number 1234567', 'Product Model Y, Serial Number 2345678', 'Product Model Z, Serial Number 3456789'] tuple_regexp_replace (['01/04/2000','06/30/2007'], '(\\d{2})/(\\d{2})/(\\d{4})', 'Day: $2, Month: $1, Year: $3', Result) *return ['Day: 04, Month: 01, Year: 2000', 'Day: 30, Month: 06, Year: 2007'] get_system ('image_dir', HalconImages) get_system ('operating_system', OS) if (OS{0:2} == 'Win') tuple_split (HalconImages, ';', HalconImagesSplit) else tuple_split (HalconImages, ':', HalconImagesSplit) endif list_files (HalconImagesSplit[0], ['files','follow_links'], Files) * 查找拓展名为png的文件 tuple_regexp_select (Files, '\\.png$', FilesPNG) * 查找拓展名为png的文件,且剔除掉文件名最后为数字的文件 tuple_regexp_select (FilesPNG, ['\\d\\.png$','invert_match'], FilesNoDigit) * 查找拓展名为png的文件,且只获取文件名称,不要完整路径,形如"8.png" tuple_regexp_match (FilesNoDigit, '[^/\\\\]*.png', ShortNames) * 更改文件的名称 tuple_regexp_replace (ShortNames, '(.*)\\.png$', 'out_$1.jpg', ConvertedNames) * Count number of files with multi-word names (name contains hyphen or underscore) tuple_regexp_test (ShortNames, '_|-', NumCombined) * *************************************************** * ***** Using regular expressions in HDevelop expressions * *************************************************** * Again count number of files with digit and calculate percentage if (|ShortNames| > 0) Result := 100.0 * regexp_test(ShortNames,'\\d') / |ShortNames| + '% of PNG file names contain a digit' endif * Return letters 2-n of all files starting with 'a' Result := regexp_match(regexp_select(ShortNames,'^a'),'^a(.*)') * The operator =~ is short for regexp_test and useful for boolean expressions if (ShortNames =~ '^z') Result := 'A filename starting with z exists' endif
标签:return,abba,tuple,正则表达式,Result,regexp,基本操作,match From: https://www.cnblogs.com/echo-efun/p/18054054