Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

matlab regular expression - coma/whitespace separated integers with header

Last post 01-03-2010, 7:45 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  12-27-2009, 11:20 PM 58100

    matlab regular expression - coma/whitespace separated integers with header

    I have a several parameters that I use as command line options in MATLAB scripts.  I want to match the coma or whitespace separated integers within the option that begins with '-fields' using one regular expression.

     

    Options Example:

    -fields 1, 2, 3

    -regions 4, 5, 6

     

    I want to match '1' and '2' and '3' only.

     

    I'm currently using two regular repressions, the first to detect the -fields option and the second to detect the coma or whitespace separated integers (?<=[,\s])(\d+)(?=$|[,\s]) with the results of the first pass.

     

    I'd prefer to use only one regular expression, but haven't been successful to date.  If you have any recommendations, please let me know.

     

    Happy Holidays,

    B

    Normal 0 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

  •  01-03-2010, 7:45 PM 58170 in reply to 58100

    Re: matlab regular expression - coma/whitespace separated integers with header

    Without knowing the regex variant that is used by MATLAB, there is a general limitation with regex patterns to do with the way test is captures by repeating match groups.

    To do the sort of thing you ask, you will need a pattern something like (untested)

    -fields(\s+(\d+),?)+

    which will use the inner match group (the '(\d+)' one) to match each of the numbers. Unfortunately, most regex variants will only return the value matched in the last use of this group. Therefore, match group #2 above will only return "3" from your example, even though it will have matched the "1" and "2" beforehand.

    (The .NET regex does get around this but I don't know if MATLAB uses this regex variant and, even if it did, whether it lets you get to the individual captures).

    Generally, this type of problem requires 2 regex patterns which are used in the way you already are.

    Susan

View as RSS news feed in XML