小探正则

前端之家收集整理的这篇文章主要介绍了小探正则前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载注:http://www.jb51.cc/article/p-hdtuzgal-cm.html

SubLime Text 2中的正则测试:

获取包信息
\bpackage\s+([\w+\.?]+)

获取取import信息
import\s+([\w+\.?]+)\;$

获取private/protected/public合并写法
但目前这里有个问题,就是无法区别:是local还是global的变量,因为as3的global的变量也是可以不写访问级别,默认为:internal的后面再考虑
\b(?:(public|private|protected)\s+)?(static)?(const)?var\s+(?:(\w+)\s*:\s*(\w+))\s*(?:,\s*(\w+)\s*:\s*(\w+))*\s*(?:\s*=\s*(\w+))?;$

获取方法信息
\b((public|private|protected)\s+)?function\s+(\w+)\s*\(\s*((\w+)\s*(:\s*(\w+))?)\s*(,\s*((\w+)\s*(:\s*(\w+))?))*\s*\)\s*:\s*void\s*((\{[.\w\W]*?\})+?)

获取类信息
\bpublic\s*class\s+(\w+)(?:(\s*(?:(extends\s+(\w+))?)\s*(?:(?:implements\s+(\w+(,\s*\w+)*))?)\s*)?)\s*\{

获取嵌入类信息
^\s*class\s+(\w+?)((\s*((extends\s+(\w+))?)\s*((implements\s+(\w+))?)\s*)?)\s*\{([.\w\W]*)\}

测试采样数据:

  1. package nParticleSys.sadfas.asdasdf.fdas.asdf {
  2. import flash.display.Bitmap;
  3. import flash.geom.Matrix;
  4.  
  5. import starling.utils.Util;
  6.  
  7. /** * [description] * @author Jave.Lin * @date 2016-7-20 * */
  8. public class Particle extends ExtCls implements IT1,IT2,IT3 {
  9. private static var _pool:Array = [];
  10.  
  11. public static function getPs():Particle {
  12. var result:Particle;
  13. if (_pool.length > 0)
  14. {
  15. result = _pool.pop();
  16. }
  17. else
  18. {
  19. result = new Particle();
  20. }
  21. return result;
  22. }
  23.  
  24. public static function recyclePs(ps:Particle):void {
  25. _pool.push(ps);
  26. }
  27.  
  28. public static function init(ps:Particle):void {
  29. ps.x = ps.y = 0;
  30. ps.angle = 0;
  31. ps.scale = 1;
  32. ps.alpha = 1;
  33.  
  34. ps.vx = ps.vy = 0;
  35. ps.angleV = 0;
  36. ps.scaleV = 0;
  37. ps.alphaV = 0;
  38.  
  39. ps.elapsedTime = 0;
  40. ps.totalTime = 1;
  41.  
  42. ps.transform = new Matrix();
  43. ps.enabled = true;
  44. }
  45.  
  46. public var x:Number,y:Number;
  47. public var angle:Number;
  48. public var scale:Number;
  49. public var alpha:Number;
  50.  
  51. public var vx:Number,vy:Number;
  52. public var angleV:Number;
  53. public var scaleV:Number;
  54. public var alphaV:Number;
  55.  
  56. public var elapsedTime:Number;
  57. public var totalTime:Number;
  58.  
  59. public var texture:Bitmap;
  60.  
  61. public var transform:Matrix;
  62.  
  63. public var enabled:Boolean;
  64.  
  65. public function applyTransform():void {
  66. transform.identity();
  67. if (texture)
  68. {
  69. if (texture.bitmapData)
  70. {
  71. transform.translate(texture.bitmapData.width * -0.5,texture.bitmapData.height * -0.5);
  72. }
  73. transform.scale(scale,scale);
  74. transform.rotate(angle);
  75. transform.translate(x,y);
  76.  
  77. texture.transform.matrix = transform;
  78.  
  79. texture.alpha = Util.clamp(alpha,0,1);
  80. }
  81. }
  82.  
  83. public function recycle():void {
  84. elapsedTime = 0;
  85. totalTime = 0;
  86. enabled = false;
  87. transform = null;
  88.  
  89. if (texture)
  90. {
  91. if (texture.parent)
  92. {
  93. texture.parent.removeChild(texture);
  94. }
  95. texture.bitmapData = null;
  96. texture = null;
  97. }
  98. }
  99. }
  100. }
  101.  
  102. class Name {
  103.  
  104. }

距离写语法分析还有很长距离;
不同的语言中,正则的一些用法还是不一样的;
如:组引用,在py中

  1. <?P<groupName1>

然后在正则中的引用

  1. <?P=groupName1>

但在别的一些语言中,对正则的封装使用可能就不一样了。

如:在as3中,就没有别名组匹配
导致在外部调用时,还得自己去数,是第几个组;
或是运行后,看结果中,是第几个组来取数据,很不方便

==================== 然后今天搜索:编译原理相关内容,发现语法解析使用一个类似:Scaner的东西,逐字符扫描处理; 最后描述出来的是一个树结构的结果:叫语法树 在描述的过程中就可以得出语法异常; 如果能成功返回语法树对象,说明已成功编译完成; 要作的就是将:语法树,转为对应执行平台需要的指令集即可;

猜你在找的正则表达式相关文章