基本操作之——正则表达式

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
复制代码

 

posted @   echo-efun  阅读(111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示