1

Using a regex I need to be extract the text between : and the comment /* so that the output is Verdana, Arial, sans-serif. Any pointers on how to approach this problem would be helpful.

: Verdana, Arial, sans-serif /*{ffDefault}*/;
neelmeg
  • 2,459
  • 6
  • 34
  • 46
  • is that an example of the complete line you would be parsing? – Clay Oct 19 '16 at 04:39
  • yes. Another possibility of the complete line would be : /*{ffDefault}*/ Verdana, Arial, sans-serif /*{ffDefault}*/; (OR) /*{ffDefault}*/ Verdana, Arial, /*{ffDefault}*/ sans-serif /*{ffDefault}*/; – neelmeg Oct 19 '16 at 04:53
  • Possible duplicate of [Learning Regular Expressions](http://stackoverflow.com/questions/4736/learning-regular-expressions) – Biffen Oct 19 '16 at 05:00

1 Answers1

2

Do:

:\s*([^/]+)\s*/\*\{[^}]*\}\*/

The only captured group is your desired portion.

  • :\s* matches a :, followed by any number of whitespaces

  • ([^/]+) matches upto the next / and put in a captured group

  • \s* matches zero or more whitespaces, followed by comment (/\*\{[^}]*\}\*/)

Demo

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • At a glance, it looks like he may be parsing a CSS file so it may not need the starting carrot. – Clay Oct 19 '16 at 04:39
  • Thanks, what if I want to ignore any comments on the same line for eg: if the string is : /*{ffDefault}*/ Verdana, Arial, sans-serif /*{ffDefault}*/; – neelmeg Oct 19 '16 at 04:54
  • @web_dev Do: `(?:\/\*\{[^}]*\}\*\/)?\s*([^\/]+)\s*\/\*\{[^}]*\}\*\/`. Check https://regex101.com/r/GdbCgj/3 – heemayl Oct 19 '16 at 05:01