Reputation: 236
Hi i am trying to match a string against a pattern
this is the possible string
signal CS, NS, dl: stateType := writeOrRead0;
signal CS, pS : stateType := writeOrRead0;
signal dS : stateType := writeOrRead0;
i am only concerned with the pattern as far as the first colon.
but the number of signals define can be more than one it could be three or four even this is the regular expression i have
^signal\\s*(\\w+),*\\s*(\\w+)\\s*:
it will pick up the second two signal but and for the second one it picks up CS and pS and but the d and S in the next signal when i use
matcher.group()
come up seperately
Can anyone give me an expression that will pick up all signal names whether there is one two three or more?
Upvotes: 0
Views: 786
Reputation: 518
You could use this regex :
^signal\s([\w,\s]*(?=:))
Starts with signal followed by a space, capturing zero or more occurences of a word followed by a comma and a space. Ending capture at the ':' but not including it into the matches.
In order to not include the signal itself in the match you can use a lookbehind as follows :
^(?<=signal)\s([\w,\s]*(?=:))
Upvotes: 0
Reputation: 17445
If you want to have a group per signal name, that's not possible (unless you have an upper bound for the number of signals, then you could write out the entire thing, but it'd be very ugly).
So you'll have to live with one group containing the names, comma separated. Then you can post-process that to get out the real signal names.
That would give something like
^signal\s+((?:\w+(?:,\s*)?)*)\s*:
(Note that I didn't escape it as a Java String.)
group 1 are your names.
Upvotes: 5
Reputation: 1830
I'd do the next thing:
1- Get the complete GROUP of signals: CS, NS, dl (for example)
2- Parse them
Your example:
1- ^signal\\s((\\w*)(?:,\\s)?)+
2- Now you have a String in a group like "CS, NS, dl" now with a simple split of ", " you can have them in an array.
Upvotes: 4