regex

Using defined strings for regex searching with python

天涯浪子 提交于 2021-02-05 09:29:17
问题 I am looking to enhance the script I have below. I am wondering if it is possible to use defined strings such as 'G', 'SG', 'PF', 'PG', 'SF', 'F', 'UTIL', 'C' to search for the Names between them and then use those strings supplied as the name of the column. The issue I have with the current set up is if a name starts with two capitals like the example below it doesn't know the difference. Being able to set the current strings to search for with regex then return the text between them I think

Wrap search phrases that may start or end with special characters and have common prefixes as whole words only with SPAN tag

本秂侑毒 提交于 2021-02-05 09:27:44
问题 I have this code to highlight words that exist in an array everything works fine except it didn't highlight the words that contain '.' spansR[i].innerHTML = t[i].replace(new RegExp(wordsArray.join("|"),'gi'), function(c) { return '<span style="color:red">'+c+'</span>'; }); I also tried to escape dot in each word for(var r=0;r<wordsArray.length;r++){ if(wordsArray[r].includes('.')){ wordsArray[r] = wordsArray[r].replace(".", "\\."); wordsArray[r] = '\\b'+wordsArray[r]+'\\b'; } } I also tried

PHP - preg_match() word after other word

冷暖自知 提交于 2021-02-05 09:27:35
问题 I have a text like this one: The cat was born on 1980 and lives ... So i want to get the cat's age with regex (the text could have more than 1 occurrence of a number with 4 digits). I'm trying this preg_match('/born on [0-9]{4}/', $text, $matches) but the result is: array('born on 1980') . I want to ignore everything before a year. Demo: http://www.phpliveregex.com/p/aYl 回答1: Group the value you want then it will be the first item of the array. $text = 'The cat was born on 1980 and lives ...'

How to split a string (using regex?) depending on digit/ not digit

99封情书 提交于 2021-02-05 09:26:34
问题 I want to split a string into a list in python, depending on digit/ not digit. For example, 5 55+6+ 5/ should return ['5','55','+','6','+','5','/'] I have some code at the moment which loops through the characters in a string and tests them using re.match("\d") or ("\D"). I was wondering if there was a better way of doing this. P.S: must be compatible with python 2.4 回答1: Assuming the + between 6 and 5 needs to be matched (which you're missing), >>> import re >>> s = '5 55+6+ 5/' >>> re

fancy-regex crate multiple matches

点点圈 提交于 2021-02-05 09:26:17
问题 I'm using the fancy-regex crate since I need lookaheads in my Regex but it seems like it's not getting all of the matches in a string like I can with the regex crate which fancy-regex is built upon: What I'm trying: use fancy_regex::Regex; let value = "Rect2(Vector2(0, 0), Vector2(0, 0))"; let re = Regex::new(r"\(([^()]*)\)").expect("Unable to create regex for values in parenthesis"); let results = re.captures(value).expect("Error running regex").expect("No matches found"); // Since 0 gets

Javascript dynamic regex

僤鯓⒐⒋嵵緔 提交于 2021-02-05 09:24:31
问题 After looking here I came up with a patter to test an array of words against a string. $.each(data, function(index, val) { var pattern = new RegExp('?:^|\s'+ val + '?=\s|$', 'g'); console.log(pattern.test(comment)); if (!pattern.test(comment)) { yay = true; } }); the problem here is that it returns true all the time. Any suggestions? Thanks! 回答1: From your JsFiddle, I forked and created one of my own, and your solution (with my regular expression from the comments) works quite well, once all

Why is this regex str_replace not working? [closed]

萝らか妹 提交于 2021-02-05 09:23:45
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 8 years ago . I am trying to use a regular expression to replace any domain in a string with another domain but it isn't working yet. I tested the RegEx part on

Regex to find string with optional spaces

纵然是瞬间 提交于 2021-02-05 09:23:27
问题 I'm trying to validate that a string has the values edit=yes edit = yes edit= yes edit =yes edit=yesonce edit = yesonce edit= yesonce edit =yesonce What I have so far matches on edit=yes but nothing more. I think my optional spaces arguments are wrong but not sure how. edit[/s]?=[/s]?[yes|yesonce] 回答1: Try this: edit\s?=\s?yes(once)? Problems with your regex: Whitespace is \s , not /s - the escape character is backslash, not slash. You don't need [] around a single character (or escaped

Why does this preg_replace call return NULL?

二次信任 提交于 2021-02-05 09:16:06
问题 Why does this call return NULL? Is the regex wrong? With the test input it doesn't return NULL. The docs say NULL indicates an error but what error could it be? $s = hex2bin('5b5d202073205b0d0a0d0a0d0a0d0a20202020202020203a'); // $s = 'test'; $s = preg_replace('/\[\](\s|.)*\]/s', '', $s); var_dump($s); // PHP 7.2.10-1+0~20181001133118.7+stretch~1.gbpb6e829 (cli) (built: Oct 1 2018 13:31:18) ( NTS ) 回答1: Your regex is causing catastrophic backtracking and causing PHP regex engine to fail. You

Trimming a part of file extension for all the files in directory - Linux

巧了我就是萌 提交于 2021-02-05 08:45:15
问题 I have a requirement in which I want to trim the file extension for all the files contained in a directory: - The file will be like; America.gz:2170 Europe.gz:2172 Africa.gz:2170 Asia.gz:2172 what I need is to trim the :2170 and :2172 from all the files, so that only the .gz extension remains. I know that with the help of below SED code, it is possible for all the entries in a file, however I need for all the files in a directory: - **sed 's/:.*//' file** Any bash or awk code to fix this will