regex

Chapter 7, Automate the boring stuff with Python, practice project: regex version of strip()

随声附和 提交于 2021-02-04 12:48:05
问题 I am reading the book "Automate the boring stuff with Python'. In Chapter 7, in the project practice: the regex version of strip(), here is my code (I use Python 3.x): def stripRegex(x,string): import re if x == '': spaceLeft = re.compile(r'^\s+') stringLeft = spaceLeft.sub('',string) spaceRight = re.compile(r'\s+$') stringRight = spaceRight.sub('',string) stringBoth = spaceRight.sub('',stringLeft) print(stringLeft) print(stringRight) else: charLeft = re.compile(r'^(%s)+'%x) stringLeft =

regex match line containing string

我是研究僧i 提交于 2021-02-04 12:11:06
问题 I'm trying to create a regex that will select an entire line where it contains a matching string. I can't seem to get it to work. Any help is greatly appreciated. You can see the test case and what I've tried here: https://www.regex101.com/r/mT5rZ3/1 Many thanks in advance! :-) 回答1: This answer solves the question with 463 steps instead of 952 steps. Just ensure a new line at the end of the file. .*Eventname 2.*\n https://www.regex101.com/r/mT5rZ3/5 回答2: If you are using the PHP regex . don't

Java Regex : match whole word with word boundary

☆樱花仙子☆ 提交于 2021-02-04 11:39:45
问题 I am trying to check whether a string contains a word as a whole, using Java. Below are some examples: Text : "A quick brown fox" Words: "qui" - false "quick" - true "quick brown" - true "ox" - false "A" - true Below is my code: String pattern = "\\b(<word>)\\b"; String s = "ox"; String text = "A quick brown fox".toLowerCase(); System.out.println(Pattern.compile(pattern.replaceAll("<word>", s.toLowerCase())).matcher(text).find()); It works fine with strings like the one I mentioned in the

Mask some part of String

你。 提交于 2021-02-04 11:33:08
问题 I have phone no and email address. I dont want to show full information. So I am thinking mask some character using Regex or MaskFormatter. Input and desired result 1) 9843444556 - 98*******6 2) test@mint.com - t***@****.com I have achieved this with String loop. But exactly I want to this by using regex or mask. Would you please kindly inform it? 回答1: Phone: String replaced = yourString.replaceAll("\\b(\\d{2})\\d+(\\d)", "$1*******$2"); Email: String replaced = yourString.replaceAll("\\b(\\w

Easy way to get Vimeo id from a vimeo url

你说的曾经没有我的故事 提交于 2021-02-04 09:14:45
问题 I'm trying to get just the id from a vimeo URL. Is there a simpler way than this? All the vimeo video urls I see are always: https://vimeo.com/29474908 https://vimeo.com/38648446 // VIMEO $vimeo = $_POST['vimeo']; function getVimeoInfo($vimeo) { $url = parse_url($vimeo); if($url['host'] !== 'vimeo.com' && $url['host'] !== 'www.vimeo.com') return false; if (preg_match('~^http://(?:www\.)?vimeo\.com/(?:clip:)?(\d+)~', $vimeo, $match)) { $id = $match[1]; } else { $id = substr($link,10,strlen(

How to split a dataframe column by the first instance of a character in its values

戏子无情 提交于 2021-02-04 08:32:22
问题 I have a dataframe (or vector?) x <- data.frame(a=c("A_B_D", "B_C")) I want to split the vector in x$a into two new columns by the first instance of "_" to get x$b [1] "A" "B_D" and x$c [2] "B" "C" i tried variants of gsub, but couldnt come to a solution. 回答1: Another option might be to use tidyr::separate : separate(x,a,into = c("b","c"),sep = "_",remove = FALSE,extra = "merge") 回答2: One idea is to replace the first _ with another delimiter and split on the new delimiter. This works because

How to split a dataframe column by the first instance of a character in its values

馋奶兔 提交于 2021-02-04 08:32:05
问题 I have a dataframe (or vector?) x <- data.frame(a=c("A_B_D", "B_C")) I want to split the vector in x$a into two new columns by the first instance of "_" to get x$b [1] "A" "B_D" and x$c [2] "B" "C" i tried variants of gsub, but couldnt come to a solution. 回答1: Another option might be to use tidyr::separate : separate(x,a,into = c("b","c"),sep = "_",remove = FALSE,extra = "merge") 回答2: One idea is to replace the first _ with another delimiter and split on the new delimiter. This works because

Python - Parsing JSON formatted text file with regex

邮差的信 提交于 2021-02-04 08:29:27
问题 I have a text file formatted like a JSON file however everything is on a single line (could be a MongoDB File). Could someone please point me in the direction of how I could extract values using a Python regex method please? The text shows up like this: {"d":{"__type":"WikiFileNodeContent:http:\/\/samplesite.com.‌​au\/ns\/business\/wi‌​ki","author":null,"d‌​escription":null,"fi‌​leAssetId":"034b9317‌​-60d9-45c2-b6d6-0f24‌​b59e1991","filename"‌​:"Reports.pdf"},"cre‌​atedBy":1531,"create‌

Extracting matches with the original case used in the pattern during a case insensitive search

久未见 提交于 2021-02-04 08:27:46
问题 While doing a regex pattern match, we get the content which has been a match. What if I want the pattern which was found in the content? See the below example: >>> import re >>> r = re.compile('ERP|Gap', re.I) >>> string = 'ERP is integral part of GAP, so erp can never be ignored, ErP!' >>> r.findall(string) ['ERP', 'GAP', 'erp', 'ErP'] but I want the output to look like this : ['ERP', 'Gap', 'ERP', 'ERP'] Because if I do a group by and sum on the original output, I would get the following

Whole word regex matching and hyperlinking in Javascript

孤街浪徒 提交于 2021-02-04 08:10:35
问题 I need a little help with Regular Expressions. I'm using Javascript and JQuery to hyperlink terms within an HTML document, to do this I'm using the following code. I'm doing this for a number of terms in a massive document. var searchterm = "Water"; jQuery('#content p').each(function() { var content = jQuery(this), txt = content.html(), found = content.find(searchterm).length, regex = new RegExp('(' + searchterm + ')(?![^(<a.*?>).]*?<\/a>)','gi'); if (found != -1) { //hyperlink the search