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)*?)*$

Answers


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)*)*)?$"

Need Your Help

How to plot graph with numbers (strings in between)?

matlab graph plot octave

I want to plot a graph with 2 arrays, One is for time (X-Axis) and the other is for current (Y-Axis) but my scenario has some events like: interrupt received, timer expired or component up.

How to reproduce a silently dropped TCP/IP connection?

java linux networking tcp

I have a situation where a Java program establishes a long-running TCP/IP connection to a server and said connection is functioning exactly as documented, except for one thing in an environment out...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.