Mercurial > octave-nkf
comparison src/DLD-FUNCTIONS/regexp.cc @ 14501:60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
* __contourc__.cc, __delaunayn__.cc, __dispatch__.cc, __dsearchn__.cc,
__fltk_uigetfile__.cc, __glpk__.cc, __lin_interpn__.cc, __magick_read__.cc,
__pchip_deriv__.cc, __qp__.cc, __voronoi__.cc, besselj.cc, betainc.cc,
bsxfun.cc, cellfun.cc, chol.cc, conv2.cc, convhulln.cc, dassl.cc, det.cc,
dlmread.cc, dmperm.cc, dot.cc, eig.cc, eigs.cc, fft.cc, fft2.cc, filter.cc,
find.cc, gammainc.cc, gcd.cc, givens.cc, hess.cc, hex2num.cc, inv.cc, kron.cc,
lookup.cc, lsode.cc, lu.cc, luinc.cc, matrix_type.cc, max.cc, mgorth.cc,
nproc.cc, qr.cc, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc,
schur.cc, spparms.cc, sqrtm.cc, str2double.cc, strfind.cc, sub2ind.cc, svd.cc,
syl.cc, time.cc, tril.cc, tsearch.cc: Update %!tests in DLD-FUNCTIONS/
directory with Octave coding conventions.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 27 Mar 2012 22:46:45 -0700 |
parents | bd592d5482c0 |
children | 6d5c951ec520 |
comparison
equal
deleted
inserted
replaced
14498:36cfbd23fe9f | 14501:60e5cf354d80 |
---|---|
647 | 647 |
648 return retval; | 648 return retval; |
649 } | 649 } |
650 | 650 |
651 /* | 651 /* |
652 | |
653 ## PCRE_ERROR_MATCHLIMIT test | 652 ## PCRE_ERROR_MATCHLIMIT test |
654 %!test | 653 %!test |
655 %! s=sprintf('\t4\n0000\t-0.00\t-0.0000\t4\t-0.00\t-0.0000\t4\n0000\t-0.00\t-0.0000\t0\t-0.00\t-'); | 654 %! s = sprintf ('\t4\n0000\t-0.00\t-0.0000\t4\t-0.00\t-0.0000\t4\n0000\t-0.00\t-0.0000\t0\t-0.00\t-'); |
656 %! ws = warning("query"); | 655 %! ws = warning ("query"); |
657 %! unwind_protect | 656 %! unwind_protect |
658 %! warning("off"); | 657 %! warning ("off"); |
659 %! regexp(s, '(\s*-*\d+[.]*\d*\s*)+\n'); | 658 %! regexp (s, '(\s*-*\d+[.]*\d*\s*)+\n'); |
660 %! unwind_protect_cleanup | 659 %! unwind_protect_cleanup |
661 %! warning(ws); | 660 %! warning (ws); |
662 %! end_unwind_protect | 661 %! end_unwind_protect |
663 | 662 |
664 ## seg-fault test | 663 ## segfault test |
665 %!assert(regexp("abcde","."),[1,2,3,4,5]) | 664 %!assert (regexp ("abcde", "."), [1,2,3,4,5]) |
666 ## Infinite loop test | 665 ## Infinite loop test |
667 %!assert (isempty (regexp("abcde", ""))) | 666 %!assert (isempty (regexp ("abcde", ""))) |
668 | 667 |
669 ## Check that anchoring of pattern works correctly | 668 ## Check that anchoring of pattern works correctly |
670 %!assert(regexp('abcabc','^abc'),1); | 669 %!assert (regexp ('abcabc', '^abc'), 1) |
671 %!assert(regexp('abcabc','abc$'),4); | 670 %!assert (regexp ('abcabc', 'abc$'), 4) |
672 %!assert(regexp('abcabc','^abc$'),zeros(1,0)); | 671 %!assert (regexp ('abcabc', '^abc$'), zeros (1,0)) |
673 | 672 |
674 %!test | 673 %!test |
675 %! [s, e, te, m, t] = regexp(' No Match ', 'f(.*)uck'); | 674 %! [s, e, te, m, t] = regexp (' No Match ', 'f(.*)uck'); |
676 %! assert (s,zeros(1,0)) | 675 %! assert (s, zeros (1,0)); |
677 %! assert (e,zeros(1,0)) | 676 %! assert (e, zeros (1,0)); |
678 %! assert (te,cell(1,0)) | 677 %! assert (te, cell (1,0)); |
679 %! assert (m, cell(1,0)) | 678 %! assert (m, cell (1,0)); |
680 %! assert (t, cell(1,0)) | 679 %! assert (t, cell (1,0)); |
681 | 680 |
682 %!test | 681 %!test |
683 %! [s, e, te, m, t] = regexp(' FiRetrUck ', 'f(.*)uck'); | 682 %! [s, e, te, m, t] = regexp (' FiRetrUck ', 'f(.*)uck'); |
684 %! assert (s,zeros(1,0)) | 683 %! assert (s, zeros (1,0)); |
685 %! assert (e,zeros(1,0)) | 684 %! assert (e, zeros (1,0)); |
686 %! assert (te,cell(1,0)) | 685 %! assert (te, cell (1,0)); |
687 %! assert (m, cell(1,0)) | 686 %! assert (m, cell (1,0)); |
688 %! assert (t, cell(1,0)) | 687 %! assert (t, cell (1,0)); |
689 | 688 |
690 %!test | 689 %!test |
691 %! [s, e, te, m, t] = regexp(' firetruck ', 'f(.*)uck'); | 690 %! [s, e, te, m, t] = regexp (' firetruck ', 'f(.*)uck'); |
692 %! assert (s,2) | 691 %! assert (s, 2); |
693 %! assert (e,10) | 692 %! assert (e, 10); |
694 %! assert (te{1},[3,7]) | 693 %! assert (te{1}, [3, 7]); |
695 %! assert (m{1}, 'firetruck') | 694 %! assert (m{1}, 'firetruck'); |
696 %! assert (t{1}{1}, 'iretr') | 695 %! assert (t{1}{1}, 'iretr'); |
697 | 696 |
698 %!test | 697 %!test |
699 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*'); | 698 %! [s, e, te, m, t] = regexp ('short test string', '\w*r\w*'); |
700 %! assert (s,[1,12]) | 699 %! assert (s, [1, 12]); |
701 %! assert (e,[5,17]) | 700 %! assert (e, [5, 17]); |
702 %! assert (size(te), [1,2]) | 701 %! assert (size (te), [1, 2]); |
703 %! assert (isempty(te{1})) | 702 %! assert (isempty (te{1})); |
704 %! assert (isempty(te{2})) | 703 %! assert (isempty (te{2})); |
705 %! assert (m{1},'short') | 704 %! assert (m{1}, 'short'); |
706 %! assert (m{2},'string') | 705 %! assert (m{2}, 'string'); |
707 %! assert (size(t), [1,2]) | 706 %! assert (size (t), [1, 2]); |
708 %! assert (isempty(t{1})) | 707 %! assert (isempty (t{1})); |
709 %! assert (isempty(t{2})) | 708 %! assert (isempty (t{2})); |
710 | 709 |
711 %!test | 710 %!test |
712 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*','once'); | 711 %! [s, e, te, m, t] = regexp ('short test string', '\w*r\w*', 'once'); |
713 %! assert (s,1) | 712 %! assert (s, 1); |
714 %! assert (e,5) | 713 %! assert (e, 5); |
715 %! assert (isempty(te)) | 714 %! assert (isempty (te)); |
716 %! assert (m,'short') | 715 %! assert (m, 'short'); |
717 %! assert (isempty(t)) | 716 %! assert (isempty (t)); |
718 | 717 |
719 %!test | 718 %!test |
720 %! [m, te, e, s, t] = regexp('short test string','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | 719 %! [m, te, e, s, t] = regexp ('short test string', '\w*r\w*', 'once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
721 %! assert (s,1) | 720 %! assert (s, 1); |
722 %! assert (e,5) | 721 %! assert (e, 5); |
723 %! assert (isempty(te)) | 722 %! assert (isempty (te)); |
724 %! assert (m,'short') | 723 %! assert (m, 'short'); |
725 %! assert (isempty(t)) | 724 %! assert (isempty (t)); |
726 | 725 |
727 %!test | 726 %!test |
728 %! [s, e, te, m, t, nm] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)'); | 727 %! [s, e, te, m, t, nm] = regexp ('short test string', '(?<word1>\w*t)\s*(?<word2>\w*t)'); |
729 %! assert (s,1) | 728 %! assert (s, 1); |
730 %! assert (e,10) | 729 %! assert (e, 10); |
731 %! assert (size(te), [1,1]) | 730 %! assert (size (te), [1, 1]); |
732 %! assert (te{1}, [1 5; 7, 10]) | 731 %! assert (te{1}, [1,5; 7,10]); |
733 %! assert (m{1},'short test') | 732 %! assert (m{1}, 'short test'); |
734 %! assert (size(t),[1,1]) | 733 %! assert (size (t), [1, 1]); |
735 %! assert (t{1}{1},'short') | 734 %! assert (t{1}{1}, 'short'); |
736 %! assert (t{1}{2},'test') | 735 %! assert (t{1}{2}, 'test'); |
737 %! assert (size(nm), [1,1]) | 736 %! assert (size (nm), [1, 1]); |
738 %! assert (!isempty(fieldnames(nm))) | 737 %! assert (! isempty (fieldnames (nm))); |
739 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | 738 %! assert (sort (fieldnames (nm)), {'word1';'word2'}); |
740 %! assert (nm.word1,'short') | 739 %! assert (nm.word1, 'short'); |
741 %! assert (nm.word2,'test') | 740 %! assert (nm.word2, 'test'); |
742 | 741 |
743 %!test | 742 %!test |
744 %! [nm, m, te, e, s, t] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | 743 %! [nm, m, te, e, s, t] = regexp ('short test string', '(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
745 %! assert (s,1) | 744 %! assert (s, 1); |
746 %! assert (e,10) | 745 %! assert (e, 10); |
747 %! assert (size(te), [1,1]) | 746 %! assert (size (te), [1, 1]); |
748 %! assert (te{1}, [1 5; 7, 10]) | 747 %! assert (te{1}, [1,5; 7,10]); |
749 %! assert (m{1},'short test') | 748 %! assert (m{1}, 'short test'); |
750 %! assert (size(t),[1,1]) | 749 %! assert (size (t), [1, 1]); |
751 %! assert (t{1}{1},'short') | 750 %! assert (t{1}{1}, 'short'); |
752 %! assert (t{1}{2},'test') | 751 %! assert (t{1}{2}, 'test'); |
753 %! assert (size(nm), [1,1]) | 752 %! assert (size (nm), [1, 1]); |
754 %! assert (!isempty(fieldnames(nm))) | 753 %! assert (!isempty (fieldnames (nm))); |
755 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | 754 %! assert (sort (fieldnames (nm)), {'word1';'word2'}); |
756 %! assert (nm.word1,'short') | 755 %! assert (nm.word1, 'short'); |
757 %! assert (nm.word2,'test') | 756 %! assert (nm.word2, 'test'); |
758 | 757 |
759 %!test | 758 %!test |
760 %! [t, nm] = regexp("John Davis\nRogers, James",'(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)','tokens','names'); | 759 %! [t, nm] = regexp ("John Davis\nRogers, James", '(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)', 'tokens', 'names'); |
761 %! assert (size(t), [1,2]); | 760 %! assert (size (t), [1, 2]); |
762 %! assert (t{1}{1},'John'); | 761 %! assert (t{1}{1}, 'John'); |
763 %! assert (t{1}{2},'Davis'); | 762 %! assert (t{1}{2}, 'Davis'); |
764 %! assert (t{2}{1},'Rogers'); | 763 %! assert (t{2}{1}, 'Rogers'); |
765 %! assert (t{2}{2},'James'); | 764 %! assert (t{2}{2}, 'James'); |
766 %! assert (size(nm), [1,1]); | 765 %! assert (size (nm), [1, 1]); |
767 %! assert (nm.first{1},'John'); | 766 %! assert (nm.first{1}, 'John'); |
768 %! assert (nm.first{2},'James'); | 767 %! assert (nm.first{2}, 'James'); |
769 %! assert (nm.last{1},'Davis'); | 768 %! assert (nm.last{1}, 'Davis'); |
770 %! assert (nm.last{2},'Rogers'); | 769 %! assert (nm.last{2}, 'Rogers'); |
771 | 770 |
772 ## Tests for named tokens | 771 ## Tests for named tokens |
773 %!test | 772 %!test |
774 %! # Parenthesis in named token (ie (int)) causes a problem | 773 %! # Parenthesis in named token (ie (int)) causes a problem |
775 %! assert (regexp('qwe int asd', ['(?<typestr>(int))'], 'names'), struct ('typestr', 'int')); | 774 %! assert (regexp ('qwe int asd', ['(?<typestr>(int))'], 'names'), struct ('typestr', 'int')); |
776 | 775 |
777 %!test | 776 %!test |
778 %! ## Mix of named and unnamed tokens can cause segfault (bug #35683) | 777 %! ## Mix of named and unnamed tokens can cause segfault (bug #35683) |
779 %! str = "abcde"; | 778 %! str = "abcde"; |
780 %! ptn = '(?<T1>a)(\w+)(?<T2>d\w+)'; | 779 %! ptn = '(?<T1>a)(\w+)(?<T2>d\w+)'; |
781 %! tokens = regexp (str, ptn, "names"); | 780 %! tokens = regexp (str, ptn, "names"); |
782 %! assert (isstruct (tokens) && numel (tokens) == 1); | 781 %! assert (isstruct (tokens) && numel (tokens) == 1); |
783 %! assert (tokens.T1, "a"); | 782 %! assert (tokens.T1, "a"); |
784 %! assert (tokens.T2, "de"); | 783 %! assert (tokens.T2, "de"); |
785 | 784 |
786 %!assert(regexp("abc\nabc",'.'),[1:7]) | 785 %!assert (regexp ("abc\nabc", '.'), [1:7]) |
787 %!assert(regexp("abc\nabc",'.','dotall'),[1:7]) | 786 %!assert (regexp ("abc\nabc", '.', 'dotall'), [1:7]) |
788 %!test | 787 %!test |
789 %! assert(regexp("abc\nabc",'(?s).'),[1:7]) | 788 %! assert (regexp ("abc\nabc", '(?s).'), [1:7]); |
790 %! assert(regexp("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) | 789 %! assert (regexp ("abc\nabc", '.', 'dotexceptnewline'), [1,2,3,5,6,7]); |
791 %! assert(regexp("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) | 790 %! assert (regexp ("abc\nabc", '(?-s).'), [1,2,3,5,6,7]); |
792 | 791 |
793 %!assert(regexp("caseCaSe",'case'),1) | 792 %!assert (regexp ("caseCaSe", 'case'), 1) |
794 %!assert(regexp("caseCaSe",'case',"matchcase"),1) | 793 %!assert (regexp ("caseCaSe", 'case', "matchcase"), 1) |
795 %!assert(regexp("caseCaSe",'case',"ignorecase"),[1,5]) | 794 %!assert (regexp ("caseCaSe", 'case', "ignorecase"), [1,5]) |
796 %!test | 795 %!test |
797 %! assert(regexp("caseCaSe",'(?-i)case'),1) | 796 %! assert (regexp ("caseCaSe", '(?-i)case'), 1); |
798 %! assert(regexp("caseCaSe",'(?i)case'),[1,5]) | 797 %! assert (regexp ("caseCaSe", '(?i)case'), [1, 5]); |
799 | 798 |
800 %!assert (regexp("abc\nabc",'c$'),7) | 799 %!assert (regexp ("abc\nabc", 'c$'), 7) |
801 %!assert (regexp("abc\nabc",'c$',"stringanchors"),7) | 800 %!assert (regexp ("abc\nabc", 'c$', "stringanchors"), 7) |
802 %!test | 801 %!test |
803 %! assert (regexp("abc\nabc",'(?-m)c$'),7) | 802 %! assert (regexp ("abc\nabc", '(?-m)c$'), 7); |
804 %! assert (regexp("abc\nabc",'c$',"lineanchors"),[3,7]) | 803 %! assert (regexp ("abc\nabc", 'c$',"lineanchors"), [3, 7]); |
805 %! assert (regexp("abc\nabc",'(?m)c$'),[3,7]) | 804 %! assert (regexp ("abc\nabc", '(?m)c$'), [3,7]); |
806 | 805 |
807 %!assert (regexp("this word",'s w'),4) | 806 %!assert (regexp ("this word", 's w'), 4) |
808 %!assert (regexp("this word",'s w','literalspacing'),4) | 807 %!assert (regexp ("this word", 's w', 'literalspacing'), 4) |
809 %!test | 808 %!test |
810 %! assert (regexp("this word",'(?-x)s w','literalspacing'),4) | 809 %! assert (regexp ("this word", '(?-x)s w', 'literalspacing'), 4); |
811 %! assert (regexp("this word",'s w','freespacing'),zeros(1,0)) | 810 %! assert (regexp ("this word", 's w', 'freespacing'), zeros (1,0)); |
812 %! assert (regexp("this word",'(?x)s w'),zeros(1,0)) | 811 %! assert (regexp ("this word", '(?x)s w'), zeros (1,0)); |
813 | 812 |
814 %!error regexp('string', 'tri', 'BadArg'); | 813 %!error regexp ('string', 'tri', 'BadArg') |
815 %!error regexp('string'); | 814 %!error regexp ('string') |
816 | 815 |
817 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) | 816 %!assert (regexp ({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'}, '-'), {6;[1,5,9];zeros(1,0)}) |
818 %!assert(regexp({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) | 817 %!assert (regexp ({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'}, {'-';'f';'q'}), {6;[3,7];[1,9]}) |
819 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) | 818 %!assert (regexp ('Strings', {'t','s'}), {2, 7}) |
820 %!assert(regexp('Strings',{'t','s'}),{2,7}) | |
821 | 819 |
822 ## Test case for lookaround operators | 820 ## Test case for lookaround operators |
823 %!test | 821 %!test |
824 %! assert(regexp('Iraq','q(?!u)'),4) | 822 %! assert (regexp ('Iraq', 'q(?!u)'), 4); |
825 %! assert(regexp('quit','q(?!u)'), zeros(1,0)) | 823 %! assert (regexp ('quit', 'q(?!u)'), zeros (1, 0)); |
826 %! assert(regexp('quit','q(?=u)','match'), {'q'}) | 824 %! assert (regexp ('quit', 'q(?=u)' , 'match'), {'q'}); |
827 %! assert(regexp("quit",'q(?=u+)','match'), {'q'}) | 825 %! assert (regexp ("quit", 'q(?=u+)', 'match'), {'q'}); |
828 %! assert(regexp("qit",'q(?=u+)','match'), cell(1,0)) | 826 %! assert (regexp ("qit", 'q(?=u+)', 'match'), cell (1, 0)); |
829 %! assert(regexp("qit",'q(?=u*)','match'), {'q'}) | 827 %! assert (regexp ("qit", 'q(?=u*)', 'match'), {'q'}); |
830 %! assert(regexp('thingamabob','(?<=a)b'), 9) | 828 %! assert (regexp ('thingamabob', '(?<=a)b'), 9); |
831 | 829 |
832 ## Tests for split option. | 830 ## Tests for split option. |
833 %!shared str | 831 %!shared str |
834 %! str = "foo bar foo"; | 832 %! str = "foo bar foo"; |
835 %!test | 833 %!test |
844 %! [a, b] = regexp (str, "fx.", "match", "split"); | 842 %! [a, b] = regexp (str, "fx.", "match", "split"); |
845 %! assert (a, cell (1, 0)); | 843 %! assert (a, cell (1, 0)); |
846 %! assert (b, {"foo bar foo"}); | 844 %! assert (b, {"foo bar foo"}); |
847 %!test | 845 %!test |
848 %! [a, b] = regexp (str, "fx.", "match", "split", "once"); | 846 %! [a, b] = regexp (str, "fx.", "match", "split", "once"); |
849 %! assert (a, ""); | 847 %! assert (a, "");; |
850 %! assert (b, "foo bar foo") | 848 %! assert (b, "foo bar foo"); |
851 | 849 |
852 %!shared str | 850 %!shared str |
853 %! str = "foo bar"; | 851 %! str = "foo bar"; |
854 %!test | 852 %!test |
855 %! [a, b] = regexp (str, "f..", "match", "split"); | 853 %! [a, b] = regexp (str, "f..", "match", "split"); |
865 %! assert (b, {"foo bar"}); | 863 %! assert (b, {"foo bar"}); |
866 %!test | 864 %!test |
867 %! [a, b] = regexp (str, "[o]+", "match", "split"); | 865 %! [a, b] = regexp (str, "[o]+", "match", "split"); |
868 %! assert (a, {"oo"}); | 866 %! assert (a, {"oo"}); |
869 %! assert (b, {"f", " bar"}); | 867 %! assert (b, {"f", " bar"}); |
870 | |
871 */ | 868 */ |
872 | 869 |
873 DEFUN_DLD (regexpi, args, nargout, | 870 DEFUN_DLD (regexpi, args, nargout, |
874 "-*- texinfo -*-\n\ | 871 "-*- texinfo -*-\n\ |
875 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}, @var{sp}] =} regexpi (@var{str}, @var{pat})\n\ | 872 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}, @var{sp}] =} regexpi (@var{str}, @var{pat})\n\ |
895 | 892 |
896 return retval; | 893 return retval; |
897 } | 894 } |
898 | 895 |
899 /* | 896 /* |
900 | 897 ## segfault test |
901 ## seg-fault test | 898 %!assert (regexpi ("abcde", "."), [1,2,3,4,5]) |
902 %!assert(regexpi("abcde","."),[1,2,3,4,5]) | |
903 | 899 |
904 ## Check that anchoring of pattern works correctly | 900 ## Check that anchoring of pattern works correctly |
905 %!assert(regexpi('abcabc','^ABC'),1); | 901 %!assert (regexpi ('abcabc', '^ABC'), 1) |
906 %!assert(regexpi('abcabc','ABC$'),4); | 902 %!assert (regexpi ('abcabc', 'ABC$'), 4) |
907 %!assert(regexpi('abcabc','^ABC$'),zeros(1,0)); | 903 %!assert (regexpi ('abcabc', '^ABC$'), zeros (1,0)) |
908 | 904 |
909 %!test | 905 %!test |
910 %! [s, e, te, m, t] = regexpi(' No Match ', 'f(.*)uck'); | 906 %! [s, e, te, m, t] = regexpi (' No Match ', 'f(.*)uck'); |
911 %! assert (s,zeros(1,0)) | 907 %! assert (s, zeros (1,0)); |
912 %! assert (e,zeros(1,0)) | 908 %! assert (e, zeros (1,0)); |
913 %! assert (te,cell(1,0)) | 909 %! assert (te, cell (1,0)); |
914 %! assert (m, cell(1,0)) | 910 %! assert (m, cell (1,0)); |
915 %! assert (t, cell(1,0)) | 911 %! assert (t, cell (1,0)); |
916 | 912 |
917 %!test | 913 %!test |
918 %! [s, e, te, m, t] = regexpi(' FiRetrUck ', 'f(.*)uck'); | 914 %! [s, e, te, m, t] = regexpi (' FiRetrUck ', 'f(.*)uck'); |
919 %! assert (s,2) | 915 %! assert (s, 2); |
920 %! assert (e,10) | 916 %! assert (e, 10); |
921 %! assert (te{1},[3,7]) | 917 %! assert (te{1}, [3, 7]); |
922 %! assert (m{1}, 'FiRetrUck') | 918 %! assert (m{1}, 'FiRetrUck'); |
923 %! assert (t{1}{1}, 'iRetr') | 919 %! assert (t{1}{1}, 'iRetr'); |
924 | 920 |
925 %!test | 921 %!test |
926 %! [s, e, te, m, t] = regexpi(' firetruck ', 'f(.*)uck'); | 922 %! [s, e, te, m, t] = regexpi (' firetruck ', 'f(.*)uck'); |
927 %! assert (s,2) | 923 %! assert (s, 2); |
928 %! assert (e,10) | 924 %! assert (e, 10); |
929 %! assert (te{1},[3,7]) | 925 %! assert (te{1}, [3, 7]); |
930 %! assert (m{1}, 'firetruck') | 926 %! assert (m{1}, 'firetruck'); |
931 %! assert (t{1}{1}, 'iretr') | 927 %! assert (t{1}{1}, 'iretr'); |
932 | 928 |
933 %!test | 929 %!test |
934 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*'); | 930 %! [s, e, te, m, t] = regexpi ('ShoRt Test String', '\w*r\w*'); |
935 %! assert (s,[1,12]) | 931 %! assert (s, [1, 12]); |
936 %! assert (e,[5,17]) | 932 %! assert (e, [5, 17]); |
937 %! assert (size(te), [1,2]) | 933 %! assert (size (te), [1, 2]); |
938 %! assert (isempty(te{1})) | 934 %! assert (isempty (te{1})); |
939 %! assert (isempty(te{2})) | 935 %! assert (isempty (te{2})); |
940 %! assert (m{1},'ShoRt') | 936 %! assert (m{1}, 'ShoRt'); |
941 %! assert (m{2},'String') | 937 %! assert (m{2}, 'String'); |
942 %! assert (size(t), [1,2]) | 938 %! assert (size (t), [1, 2]); |
943 %! assert (isempty(t{1})) | 939 %! assert (isempty (t{1})); |
944 %! assert (isempty(t{2})) | 940 %! assert (isempty (t{2})); |
945 | 941 |
946 %!test | 942 %!test |
947 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*','once'); | 943 %! [s, e, te, m, t] = regexpi ('ShoRt Test String', '\w*r\w*', 'once'); |
948 %! assert (s,1) | 944 %! assert (s, 1); |
949 %! assert (e,5) | 945 %! assert (e, 5); |
950 %! assert (isempty(te)) | 946 %! assert (isempty (te)); |
951 %! assert (m,'ShoRt') | 947 %! assert (m, 'ShoRt'); |
952 %! assert (isempty(t)) | 948 %! assert (isempty (t)); |
953 | 949 |
954 %!test | 950 %!test |
955 %! [m, te, e, s, t] = regexpi('ShoRt Test String','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | 951 %! [m, te, e, s, t] = regexpi ('ShoRt Test String', '\w*r\w*', 'once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
956 %! assert (s,1) | 952 %! assert (s, 1); |
957 %! assert (e,5) | 953 %! assert (e, 5); |
958 %! assert (isempty(te)) | 954 %! assert (isempty (te)); |
959 %! assert (m,'ShoRt') | 955 %! assert (m, 'ShoRt'); |
960 %! assert (isempty(t)) | 956 %! assert (isempty (t)); |
961 | 957 |
962 %!test | 958 %!test |
963 %! [s, e, te, m, t, nm] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)'); | 959 %! [s, e, te, m, t, nm] = regexpi ('ShoRt Test String', '(?<word1>\w*t)\s*(?<word2>\w*t)'); |
964 %! assert (s,1) | 960 %! assert (s, 1); |
965 %! assert (e,10) | 961 %! assert (e, 10); |
966 %! assert (size(te), [1,1]) | 962 %! assert (size (te), [1, 1]); |
967 %! assert (te{1}, [1 5; 7, 10]) | 963 %! assert (te{1}, [1,5; 7,10]); |
968 %! assert (m{1},'ShoRt Test') | 964 %! assert (m{1}, 'ShoRt Test'); |
969 %! assert (size(t),[1,1]) | 965 %! assert (size (t), [1, 1]); |
970 %! assert (t{1}{1},'ShoRt') | 966 %! assert (t{1}{1}, 'ShoRt'); |
971 %! assert (t{1}{2},'Test') | 967 %! assert (t{1}{2}, 'Test'); |
972 %! assert (size(nm), [1,1]) | 968 %! assert (size (nm), [1, 1]); |
973 %! assert (!isempty(fieldnames(nm))) | 969 %! assert (! isempty (fieldnames (nm))); |
974 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | 970 %! assert (sort (fieldnames (nm)), {'word1';'word2'}); |
975 %! assert (nm.word1,'ShoRt') | 971 %! assert (nm.word1, 'ShoRt'); |
976 %! assert (nm.word2,'Test') | 972 %! assert (nm.word2, 'Test'); |
977 | 973 |
978 %!test | 974 %!test |
979 %! [nm, m, te, e, s, t] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | 975 %! [nm, m, te, e, s, t] = regexpi ('ShoRt Test String', '(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
980 %! assert (s,1) | 976 %! assert (s, 1); |
981 %! assert (e,10) | 977 %! assert (e, 10); |
982 %! assert (size(te), [1,1]) | 978 %! assert (size (te), [1, 1]); |
983 %! assert (te{1}, [1 5; 7, 10]) | 979 %! assert (te{1}, [1,5; 7,10]); |
984 %! assert (m{1},'ShoRt Test') | 980 %! assert (m{1}, 'ShoRt Test'); |
985 %! assert (size(t),[1,1]) | 981 %! assert (size (t), [1, 1]); |
986 %! assert (t{1}{1},'ShoRt') | 982 %! assert (t{1}{1}, 'ShoRt'); |
987 %! assert (t{1}{2},'Test') | 983 %! assert (t{1}{2}, 'Test'); |
988 %! assert (size(nm), [1,1]) | 984 %! assert (size (nm), [1, 1]); |
989 %! assert (!isempty(fieldnames(nm))) | 985 %! assert (!isempty (fieldnames (nm))); |
990 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | 986 %! assert (sort (fieldnames (nm)), {'word1';'word2'}); |
991 %! assert (nm.word1,'ShoRt') | 987 %! assert (nm.word1, 'ShoRt'); |
992 %! assert (nm.word2,'Test') | 988 %! assert (nm.word2, 'Test'); |
993 | 989 |
994 %!assert(regexpi("abc\nabc",'.'),[1:7]) | 990 %!assert (regexpi ("abc\nabc", '.'), [1:7]) |
995 %!assert(regexpi("abc\nabc",'.','dotall'),[1:7]) | 991 %!assert (regexpi ("abc\nabc", '.', 'dotall'), [1:7]) |
996 %!test | 992 %!test |
997 %! assert(regexpi("abc\nabc",'(?s).'),[1:7]) | 993 %! assert (regexpi ("abc\nabc", '(?s).'), [1:7]); |
998 %! assert(regexpi("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) | 994 %! assert (regexpi ("abc\nabc", '.', 'dotexceptnewline'), [1,2,3,5,6,7]); |
999 %! assert(regexpi("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) | 995 %! assert (regexpi ("abc\nabc", '(?-s).'), [1,2,3,5,6,7]); |
1000 | 996 |
1001 %!assert(regexpi("caseCaSe",'case'),[1,5]) | 997 %!assert (regexpi ("caseCaSe", 'case'), [1, 5]) |
1002 %!assert(regexpi("caseCaSe",'case',"matchcase"),1) | 998 %!assert (regexpi ("caseCaSe", 'case', "matchcase"), 1) |
1003 %!assert(regexpi("caseCaSe",'case',"ignorecase"),[1,5]) | 999 %!assert (regexpi ("caseCaSe", 'case', "ignorecase"), [1, 5]) |
1004 %!test | 1000 %!test |
1005 %! assert(regexpi("caseCaSe",'(?-i)case'),1) | 1001 %! assert (regexpi ("caseCaSe", '(?-i)case'), 1); |
1006 %! assert(regexpi("caseCaSe",'(?i)case'),[1,5]) | 1002 %! assert (regexpi ("caseCaSe", '(?i)case'), [1, 5]); |
1007 | 1003 |
1008 %!assert (regexpi("abc\nabc",'C$'),7) | 1004 %!assert (regexpi ("abc\nabc", 'C$'), 7) |
1009 %!assert (regexpi("abc\nabc",'C$',"stringanchors"),7) | 1005 %!assert (regexpi ("abc\nabc", 'C$', "stringanchors"), 7) |
1010 %!test | 1006 %!test |
1011 %! assert (regexpi("abc\nabc",'(?-m)C$'),7) | 1007 %! assert (regexpi ("abc\nabc", '(?-m)C$'), 7); |
1012 %! assert (regexpi("abc\nabc",'C$',"lineanchors"),[3,7]) | 1008 %! assert (regexpi ("abc\nabc", 'C$', "lineanchors"), [3, 7]); |
1013 %! assert (regexpi("abc\nabc",'(?m)C$'),[3,7]) | 1009 %! assert (regexpi ("abc\nabc", '(?m)C$'), [3, 7]); |
1014 | 1010 |
1015 %!assert (regexpi("this word",'S w'),4) | 1011 %!assert (regexpi ("this word", 'S w'), 4) |
1016 %!assert (regexpi("this word",'S w','literalspacing'),4) | 1012 %!assert (regexpi ("this word", 'S w', 'literalspacing'), 4) |
1017 %!test | 1013 %!test |
1018 %! assert (regexpi("this word",'(?-x)S w','literalspacing'),4) | 1014 %! assert (regexpi ("this word", '(?-x)S w', 'literalspacing'), 4); |
1019 %! assert (regexpi("this word",'S w','freespacing'),zeros(1,0)) | 1015 %! assert (regexpi ("this word", 'S w', 'freespacing'), zeros (1,0)); |
1020 %! assert (regexpi("this word",'(?x)S w'),zeros(1,0)) | 1016 %! assert (regexpi ("this word", '(?x)S w'), zeros (1,0)); |
1021 | 1017 |
1022 %!error regexpi('string', 'tri', 'BadArg'); | 1018 %!error regexpi ('string', 'tri', 'BadArg') |
1023 %!error regexpi('string'); | 1019 %!error regexpi ('string') |
1024 | 1020 |
1025 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) | 1021 %!assert (regexpi ({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'}, '-'), {6;[1,5,9];zeros(1, 0)}) |
1026 %!assert(regexpi({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) | 1022 %!assert (regexpi ({'asdfg-dfd', '-dfd-dfd-', 'qasfdfdaq'}, '-'), {6, [1,5,9], zeros(1,0)}) |
1027 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) | 1023 %!assert (regexpi ({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'}, {'-';'f';'q'}), {6;[3,7];[1,9]}) |
1028 %!assert(regexpi('Strings',{'t','s'}),{2,[1,7]}) | 1024 %!assert (regexpi ('Strings', {'t', 's'}), {2, [1, 7]}) |
1029 | |
1030 */ | 1025 */ |
1031 | |
1032 | 1026 |
1033 static octave_value | 1027 static octave_value |
1034 octregexprep (const octave_value_list &args, const std::string &who) | 1028 octregexprep (const octave_value_list &args, const std::string &who) |
1035 { | 1029 { |
1036 octave_value retval; | 1030 octave_value retval; |
1194 } | 1188 } |
1195 | 1189 |
1196 /* | 1190 /* |
1197 %!test # Replace with empty | 1191 %!test # Replace with empty |
1198 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | 1192 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; |
1199 %! t = regexprep(xml,'<[!?][^>]*>',''); | 1193 %! t = regexprep (xml, '<[!?][^>]*>', ''); |
1200 %! assert(t,' <tag v="hello">some stuff</tag>') | 1194 %! assert (t, ' <tag v="hello">some stuff</tag>'); |
1201 | 1195 |
1202 %!test # Replace with non-empty | 1196 %!test # Replace with non-empty |
1203 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | 1197 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; |
1204 %! t = regexprep(xml,'<[!?][^>]*>','?'); | 1198 %! t = regexprep (xml, '<[!?][^>]*>', '?'); |
1205 %! assert(t,'? <tag v="hello">some stuff?</tag>') | 1199 %! assert (t, '? <tag v="hello">some stuff?</tag>'); |
1206 | 1200 |
1207 %!test # Check that 'tokenize' is ignored | 1201 %!test # Check that 'tokenize' is ignored |
1208 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | 1202 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; |
1209 %! t = regexprep(xml,'<[!?][^>]*>','','tokenize'); | 1203 %! t = regexprep (xml, '<[!?][^>]*>', '', 'tokenize'); |
1210 %! assert(t,' <tag v="hello">some stuff</tag>') | 1204 %! assert (t, ' <tag v="hello">some stuff</tag>'); |
1211 | 1205 |
1212 ## Test capture replacement | 1206 ## Test capture replacement |
1213 %!test | 1207 %!test |
1214 %! data = "Bob Smith\nDavid Hollerith\nSam Jenkins"; | 1208 %! data = "Bob Smith\nDavid Hollerith\nSam Jenkins"; |
1215 %! result = "Smith, Bob\nHollerith, David\nJenkins, Sam"; | 1209 %! result = "Smith, Bob\nHollerith, David\nJenkins, Sam"; |
1216 %! t = regexprep(data,'(?m)^(\w+)\s+(\w+)$','$2, $1'); | 1210 %! t = regexprep (data, '(?m)^(\w+)\s+(\w+)$', '$2, $1'); |
1217 %! assert(t,result) | 1211 %! assert (t, result); |
1218 | 1212 |
1219 ## Return the original if no match | 1213 ## Return the original if no match |
1220 %!assert(regexprep('hello','world','earth'),'hello') | 1214 %!assert (regexprep ('hello', 'world', 'earth'), 'hello') |
1221 | 1215 |
1222 ## Test a general replacement | 1216 ## Test a general replacement |
1223 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_"), "a_b_c_d_e_f_g"); | 1217 %!assert (regexprep ("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_"), "a_b_c_d_e_f_g") |
1224 | 1218 |
1225 ## Make sure it works at the beginning and end | 1219 ## Make sure it works at the beginning and end |
1226 %!assert(regexprep("a[b]c{d}e-f=g", "a", "_"), "_[b]c{d}e-f=g"); | 1220 %!assert (regexprep ("a[b]c{d}e-f=g", "a", "_"), "_[b]c{d}e-f=g") |
1227 %!assert(regexprep("a[b]c{d}e-f=g", "g", "_"), "a[b]c{d}e-f=_"); | 1221 %!assert (regexprep ("a[b]c{d}e-f=g", "g", "_"), "a[b]c{d}e-f=_") |
1228 | 1222 |
1229 ## Options | 1223 ## Options |
1230 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_", "once"), "a_b]c{d}e-f=g"); | 1224 %!assert (regexprep ("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_", "once"), "a_b]c{d}e-f=g") |
1231 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "ignorecase"), "a_b_c_d_e_f_g"); | 1225 %!assert (regexprep ("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "ignorecase"), "a_b_c_d_e_f_g") |
1232 | 1226 |
1233 ## Option combinations | 1227 ## Option combinations |
1234 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "once", "ignorecase"), "a_b]c{d}e-f=g"); | 1228 %!assert (regexprep ("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "once", "ignorecase"), "a_b]c{d}e-f=g") |
1235 | 1229 |
1236 ## End conditions on replacement | 1230 ## End conditions on replacement |
1237 %!assert(regexprep("abc","(b)",".$1"),"a.bc"); | 1231 %!assert (regexprep ("abc", "(b)", ".$1"), "a.bc"); |
1238 %!assert(regexprep("abc","(b)","$1"),"abc"); | 1232 %!assert (regexprep ("abc", "(b)", "$1"), "abc"); |
1239 %!assert(regexprep("abc","(b)","$1."),"ab.c"); | 1233 %!assert (regexprep ("abc", "(b)", "$1."), "ab.c"); |
1240 %!assert(regexprep("abc","(b)","$1.."),"ab..c"); | 1234 %!assert (regexprep ("abc", "(b)", "$1.."), "ab..c"); |
1241 | 1235 |
1242 ## Test cell array arguments | 1236 ## Test cell array arguments |
1243 %!assert(regexprep("abc",{"b","a"},"?"),"??c") | 1237 %!assert (regexprep ("abc", {"b","a"}, "?"), "??c") |
1244 %!assert(regexprep({"abc","cba"},"b","?"),{"a?c","c?a"}) | 1238 %!assert (regexprep ({"abc","cba"}, "b", "?"), {"a?c","c?a"}) |
1245 %!assert(regexprep({"abc","cba"},{"b","a"},{"?","!"}),{"!?c","c?!"}) | 1239 %!assert (regexprep ({"abc","cba"}, {"b","a"}, {"?","!"}), {"!?c","c?!"}) |
1246 | 1240 |
1247 # Nasty lookbehind expression | 1241 # Nasty lookbehind expression |
1248 %!test | 1242 %!assert (regexprep ('x^(-1)+y(-1)+z(-1)=0', '(?<=[a-z]+)\(\-[1-9]*\)', '_minus1'),'x^(-1)+y_minus1+z_minus1=0') |
1249 %! assert(regexprep('x^(-1)+y(-1)+z(-1)=0','(?<=[a-z]+)\(\-[1-9]*\)','_minus1'),'x^(-1)+y_minus1+z_minus1=0') | |
1250 | |
1251 */ | 1243 */ |