C/C++ 正则表达式

在头文件 regex 中有关于正则表达式的实现和接口。

cmatch、smatch 类保存匹配结果。

参考C++标准库中正则表达式的使用

JavaScript 的BLOB

Blob(Binary Large Object)术语最初来自数据库,早期数据库因为要存储声音、图片、以及可执行程序等二进制数据对象所以给该类对象取名为Blob。

Blob 对象表示一个不可变、原始数据的类文件对象。Blob 表示的不一定是JavaScript原生格式的数据。File 接口基于Blob,继承了 blob 的功能并将其扩展使其支持用户系统上的文件。

要从其他非blob对象和数据构造一个Blob,请使用 Blob() 构造函数。要创建包含另一个blob数据的子集blob,请使用 slice()方法。文件继承自Blob。

BLOB 类的构造函数、熟悉和方法参考 MDN

一个关于BLOB的博客 显示本地图片文件

var input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.multiple = true;
input.style.display = 'none';
document.body.appendChild(input);

var fileSelect = document.createElement('a');
fileSelect.href = '#';
fileSelect.appendChild(document.createTextNode('Choose files'));
document.body.appendChild(fileSelect);

var imgList = document.createElement('div');
imgList.innerHTML = '<p > No file Selected!</p >';
document.body.appendChild(imgList);

input.addEventListener('change',
    function(e){
        var files = this.files;
        if(files.length == 0){
            return ;
        }else{
            imgList.innerHTML = '';
            var ls = document.createElement('ul');
            imgList.appendChild(ls);
            for(var i = 0;i < files.length; i++){
            var li = document.createElement('li');
            ls.appendChild(li);

            var img = document.createElement('img');
            img.src = window.URL.createObjectURL(files[i]);
            img.height = 60;
            img.width = 60;
            img.onload = function(){
                window.URL.revokeObjectURL(img.src);
            };
            li.appendChild(img);
            var info = document.createElement('span');
            info.innerHTML = files[i].name + ':' + files[i].size + ' bytes.';
            li.appendChild(info);
            }
        }
    }, 
    false);
fileSelect.addEventListener('click',
function(e){
    input.click();
    e.preventDefault();
},
false);

PowerShell 的正则表达式

PowerShell 使用Regex 类来实现正则表达式,命名空间是 System.Text.RegularExpressions。在PowerShell 中可直接使用regex。

Regex 类的常用方法。

IsMatch
通过调用 IsMatch 方法确定输入文本中是否具有正则表达式模式匹配项。
Regex.IsMatch(string, pattern)
Match、Matches
通过调用 Match 或 Matches 方法检索匹配正则表达式模式的一个或所有文本匹配项。
第一个方法返回提供有关匹配文本的信息的 Match 对象。 第二个方法返回 MatchCollection 对象,该对象对于在分析的文本中找到的每个匹配项包含一个 Match 对象。
Replace
通过调用 Replace 方法替换匹配正则表达式模式的文本。
$p = New-Object System.Text.RegularExpressions.Regex('(?<=href\s*?=\s*?").*?(?=" *)') 
$p = [regex]('(?<=href\s*?=\s*?").*?(?=" *)')
$html = @"
        <nav class="navbar navbar-default">
            ......    
        </div>
"@
$r =[System.Text.RegularExpressions.Regex]::Matches($html, $p)
$r = [regex]::Matches($html, $p)
$r|ForEach-Object{$_.value}

Python 字符串替换

字符串对象提供replace 方法。
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
re 模块提供 sub 方法,实现字符串的文本替换,sub 是 substitue 的缩写,意思是替换。
语法是:re.sub(pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function; if it is a string, any backslash escapes in it are processed. That is, \n is converted to a single newline character, \r is converted to a carriage return, and so forth. Unknown escapes such as \& are left alone. Backreferences, such as \6, are replaced with the substring matched by group 6 in the pattern.
If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string
参考地址 re.sub

替换掉文件名在的/、\、(、)、" 符号为-。

import re

p = re.compile(r'[/\\()"]')
newFileName = re.sub(p, '_', oldFileName)

总结

正则表达式都是第三方类(库,模块)提供支持,模式字符串的字面量书写会有细微差别,JS和Python 感觉最好;类库都提供了模式匹配、查找、替换的方法,JS主要是字符串提供方法。