regex

open file and read sentence

流过昼夜 提交于 2021-02-18 07:34:07
问题 I want to open a file and get sentences. The sentences in the file go across lines, like this: "He said, 'I'll pay you five pounds a week if I can have it on my own terms.' I'm a poor woman, sir, and Mr. Warren earns little, and the money meant much to me. He took out a ten-pound note, and he held it out to me then and there. currently I'm using this code: text = ' '.join(file_to_open.readlines()) sentences = re.split(r' *[\.\?!][\'"\)\]]* *', text) readlines cuts through the sentences, is

Extracting address elements from a String using NSDataDetector in Swift 3.0

大憨熊 提交于 2021-02-18 06:58:15
问题 I'm attempting to use NSDataDetector to addresses from a string. I've taken a look at NSHipster's article on NSDataDetector as well as Apple's NSDataDetector documentation. I've got the following method to the point where it'll pull addresses out of a string: func getAddress(from dataString: String) -> [String] { let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue) let matches = detector.matches(in: dataString, options: [], range: NSRange(location: 0,

Replace non alphanumeric characters except some exceptions python

馋奶兔 提交于 2021-02-18 05:26:22
问题 In perl s/[^\w:]//g would replace all non alphanumeric characters EXCEPT : In python I'm using re.sub(r'\W+', '',mystring) which does remove all non alphanumeric except _ underscore. Is there any way to put exceptions, I wish not to replace signs like = and . Previously I was applying the other approach i.e. to replace all unwanted characters using re.sub('[!@#\'\" $()]', '',mystring`) However, it is not possible for me to predict what all characters may come in mystring hence I wish to

Replace non alphanumeric characters except some exceptions python

自古美人都是妖i 提交于 2021-02-18 05:26:13
问题 In perl s/[^\w:]//g would replace all non alphanumeric characters EXCEPT : In python I'm using re.sub(r'\W+', '',mystring) which does remove all non alphanumeric except _ underscore. Is there any way to put exceptions, I wish not to replace signs like = and . Previously I was applying the other approach i.e. to replace all unwanted characters using re.sub('[!@#\'\" $()]', '',mystring`) However, it is not possible for me to predict what all characters may come in mystring hence I wish to

python笔试题

夙愿已清 提交于 2021-02-18 01:50:39
1、python中is和==的区别 A.Python中对象包含的三个基本要素,分别是:id(身份标识) 、type(数据类型)和value(值)。 B.‘==’比较的是value值 C.‘is’比较的是id 2、简述read、readline、readlines的区别 read读取整个文件 readline读取下一行数据 readlines读取整个文件到一个迭代器以供我们遍历(读取 到一个list中,以供使用,比较方便) 3、举例说明创建字典的至少两种方法 # 1 dict1 = {key1:v1,key2:v2} # 2 dict2 = {} dict2[key1] = v1 dict2[key2] = v2 # 3 dict3 = dict(key1=v1,key2=v2) 4、*args,**kwargs的作用是什么?如何使用? *args和**kwargs通常使用在函数定义里,*args允许函数传入不定量个数的非关键字参数,**kwargs允许函数传入不定量个数的关键字参数 5、python中match()和search()的区别? match()函数只检测RE是不是在string的开始位置匹配, search()会扫描整个string查找匹配, 也就是说match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话,match()就返回none 6

Guide on how to use regex in Nginx location block section?

风流意气都作罢 提交于 2021-02-17 08:28:56
问题 Nginx regex location syntaxe Regex expressions can be used with Nginx location block section, this is implemented with the PCRE engine. What does exactly this feature support as it is not fully documented? 回答1: Nginx location: Nginx location block section have a search order, a modifier, an implicit match type and an implicit switch to whether stop the search on match or not. the following array describe it for regex. # -------------------------------------------------------------------------

How to do case insensitive RegEx in Java? [duplicate]

删除回忆录丶 提交于 2021-02-17 07:19:06
问题 This question already has answers here : Regex match entire words only (7 answers) Closed 4 years ago . I have a private method that I use for finding drug name using RegEx. The code is as following, private boolean containsExactDrugName(String testString, String drugName) { int begin = -1; int end = -1; Matcher m = Pattern.compile("\\b(?:" + drugName + ")\\b|\\S+", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(testString); ArrayList<String> results = new ArrayList<>(); while (m

replaceAll Java method to remove “\\n” from String [duplicate]

梦想与她 提交于 2021-02-17 07:14:41
问题 This question already has answers here : How to remove the backslash in string using regex in Java? (3 answers) Closed 2 years ago . I have a simple treatement but I'm stuck I have something like "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99"; public static void main(String[] args) { String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99"; String filtre1 = value.replaceAll("\"", "");

Java regex match across words having apostrophe

痞子三分冷 提交于 2021-02-17 07:03:26
问题 I have a sentence "no it's all right lag" where I am trying to detected a pattern "no lag". My regex is no + (\\s\\w*?\\s?){1,3} + lag . This fails. However if my sentence is "no its all right lag" (note that the word its is not having the apostrophe), then the match succeeds. Can anyone suggest how can i ignore the apostrophe in the window. I am using java pattern matcher . 回答1: You could create a new character class with the [] notation, instead of having \w you should use [\w'] making the

Regexp - select everything before the 3rd slash

让人想犯罪 __ 提交于 2021-02-17 06:58:25
问题 What would be the easiest regexp selecting all before the 3rd slash I tried this: ([^\/?#]+){3}(?:.*?\/) But it does not work exactly as I would hope it would do. Whats more i dont know that it will work in Google Analytics (Filter Section) 回答1: What you might do instead is ot repeat matching 2 times not a forward slash and then a forward slash. ^(?:[^\/]*\/){2}[^\/]+ See a regex demo If you don't want to match ?# you could add that to the character class ^(?:[^\/?#]*\/){2}[^\/]+ About your