0

I have

^\s*(\w+(\,\w+)*\s*(:|,|\()(\(|\[)?\s*)

Which works great in extracting "commands" from two set of strings like this

**Strict pattern**
command:some text
command: some text
command,command2: some text

**Loose pattern**
command :some text
command( some text )
command, some txt

Here is link for more clarity in my problem. http://rubular.com/r/epTIiU32Dj

Can you please write two separate RegExp for strict and lose pattern?

So that using one RegExp, I just get strict-pattern-commands and using other I just get lose-pattern-commands.

Whenever, I am trying something, I end up overstepping each other pattern. I am not able to get a working RegExp for each pattern.

Watt
  • 3,118
  • 14
  • 54
  • 85

1 Answers1

1

First of all, you can shorten your regex a bit by using character classes instead of groups and alternation (http://www.rubular.com/r/pI7dk7qzxS):

^\s*(\w+(,\w+)*\s*[:,(\[]\s*)


Here is the strict pattern (http://www.rubular.com/r/xRIQuAR6TM):

^\s*(\w+(,\w+)*:\s*)


And here is the loose pattern (http://www.rubular.com/r/9ztWBYsj1a):

^\s*(\w+(,\w+)*(\s+:|\s*[,(\[]\s+)\s*)

Note that these work for the example data you gave (as you can see in the Rubular links), but it was difficult to tell just from the sample data and your regex if this will cover all of the potential cases, if there are issues you will need to explain the grammar more thoroughly.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306