Mikatsu
Mikatsu

Reputation: 550

Parsing strings with regex into different patterns

Could someone show me a correct syntax. The complete string should consist of only numbers and characters and whitespaces in each pattern except in type. The type and one parm are mandatory. The whitespaces before and after > : are mandatory. The whitespaces after , are mandatory. Several parameters as well as nodes are optional.

GOOD
type : param
type : par am, para m > no de
type : param > node
type : param, param2 > node
type : param, param2, param3 > node

NOTGOOD
type:                             <<< no param
type: param                       <<< because no whitespace after type
type : param,                     <<< , to much
type : param, param, > node       <<< 1 , after second param to much
type : param, param >> node       <<< 1 > to much

Thats what i ve now:

^(?<type>(\w+(\s)*))\s+:\s+(?<params>.+\S*?)(?<node>\s+>\s+(\w|\s)*?)*$

Upvotes: 0

Views: 102

Answers (1)

Brian Stephens
Brian Stephens

Reputation: 5271

Try this regex:

"^(?<type>\w+)\s+:\s+(?<params>\w[\s\w]*(,\s+\w[\s\w]*)*)(\s+>\s+(?<node>\w[\s\w]*)*)?$"

You didn't give an example with multiple nodes, so I didn't know how they would look. This only allows spaces in-between them. If they should have commas, then make the "node" matching group the same as the "params" matching group.

Based on your latest comments, it seems that each param and node can have multiple "words" but no leading or trailing spaces. This also implies that there's only one whitespace character after each :, ,, and >. If this is true, then try this variation of the regex:

"^(?<type>\w+)\s+:\s(?<params>\w([\s\w]*\w)*(,\s\w([\s\w]*\w)*)*)(\s>\s(?<node>\w([\s\w]*\w)*)*)?$"

Upvotes: 2

Related Questions