Halcon笔记

Halcon笔记之正则表达式

* This program demonstrates the tuple operators and functions for working
* with regular expressions.
* 
* ***************************************************
* ***** Regular expression basics
* ***************************************************
tuple_regexp_match ('abba', 'ab*', Matches)
tuple_regexp_match ('abba', 'ba*', Matches)
tuple_regexp_match ('abba', 'b+a*', Matches)
tuple_regexp_test ('ababab', '(ab){3}', NumMatches)
tuple_regexp_test ('abababa', '(ab){3}', NumMatches)
tuple_regexp_test ('abababa', '^(ab){3}$', NumMatches)
tuple_regexp_replace ('abba', 'b*', 'x', Result)
tuple_regexp_replace ('abba', 'b', 'x', Result)
tuple_regexp_replace ('abba', ['b','replace_all'], 'x', Result)
* ***************************************************
* ***** Some sample expressions
* ***************************************************
tuple_regexp_replace (['SN/1234567-X','SN/2345678-Y','SN/3456789-Z'], 'SN/(\\d{7})-([A-Z])', 'Product Model $2, Serial Number $1', Result)
tuple_regexp_replace (['01/04/2000','06/30/2007'], '(\\d{2})/(\\d{2})/(\\d{4})', 'Day: $2, Month: $1, Year: $3', Result)
* ***************************************************
* ***** Working with file names
* ***************************************************
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)
* Filter list of files by extension PNG
tuple_regexp_select (Files, '\\.png$', FilesPNG)
* Ignore images sets by removing all files which end with a digit
tuple_regexp_select (FilesPNG, ['\\d\\.png$','invert_match'], FilesNoDigit)
* Extract file names without slashes (strip directory part)
tuple_regexp_match (FilesNoDigit, '[^/\\\\]*.png', ShortNames)
* Transform file names, e.g., for creating processed output files
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

 

你可能感兴趣的:(Halcon)