regex

Python Regex Sub with Multiple Patterns

北城余情 提交于 2021-01-28 00:31:19
问题 I'm trying to match multiple patterns using regex sub grouping and replace the match with an asterisk for a data file that has similar format to the string below. However, I am getting only the desired results for the first match. The subsequent matches are consuming string that I did not expect. Is there a better approach to getting the desired output below? import re myString = '-fruit apple -number 123 -animal cat -name bob' match = re.compile('(-fruit\s+)(\w+)|' '(-animal\s+)(cat)|' '(

ng-pattern to allow alpha and certain special characters

二次信任 提交于 2021-01-28 00:10:33
问题 In form - name field I want to do a validation against Should allow only Alpha characters Allow space Allow certain special characters - _ \ / - . ’ I tried, ng-pattern="/^[a-z]+[_+\+/+-+.+’][a-z]*$/" Looks like I have missed out something as its not working! 回答1: Let's see: Should allow only Alpha characters => [a-zA-Z] Allow space => [ ] or \s Allow certain special characters - _ \ / - . ’ ' => [_\\\/.’'-] So, combining all that, the pattern will look like: ng-pattern="/^[a-zA-Z _\\\/.’'-]+

Modulo operator in a regular expression

送分小仙女□ 提交于 2021-01-27 22:43:46
问题 I am attempting to write a regular expression to accept any binary strings with the only criteria being that the number of 0s is not a factor of 3 ([number of 0s] % 3 != 0). How can this be achieved? 回答1: You can use .match() to achieve this. .match() returns an array of all occurrences matching the regex. Using modulo on the returned array's .length will tell you if the number of 0s is divisible by 3. var someString = '012345167891abcd1efghi1jklmn'; var numOfOnes = someString.match(/(1)/g) /

Extract the sub string matching regex

醉酒当歌 提交于 2021-01-27 22:03:25
问题 I am trying to extract 22 chocolates from the following string: SOMETEXT for 2 FFXX. Another 22 chocolates & 45 chamkila. using regex \\d+\\s*(chocolates.|chocolate.) . I used : grep("\\d+\\s*(chocolates.|chocolate.)",s) but it does not give the string 22 chocolates . How could I extract the part that is matching the regex? 回答1: Here is an option using sub from base R: x <- "SOMETEXT for 2 FFXX. Another 22 chocolates & 45 chamkila." sub(".*?(\\d+ chocolates?).*", "\\1", x) 22 chocolates The

How to match lowercase letters and a selection of punctuation characters?

佐手、 提交于 2021-01-27 21:57:23
问题 Desired Behaviour I have an input field (a form's textarea) where I want to allow the following characters: lowercase letter single space single commma single apostrophe single dash Basically, just allowing people to freely type text within the above constraints. Actual Behaviour The pattern I have created does not catch defined punctuation at all, or as desired: commas are not being matched single apostrophe is matched, but so are multiple apostrophe's dashes are not being matched What I've

Replace a string that matches a pattern using preg_replace_callback

主宰稳场 提交于 2021-01-27 21:41:59
问题 I want to replace the occurrences of the pattern "binary_function([x,y])" with substring "XY" in a given string. I have it working with the following code: // $string is the string to be searched $string = preg_replace_callback('/binary_function\(\[(\S),(\S)\]\)/', function ($word) { $result = strtoupper($word[1]) . strtoupper($word[2]); return $result; }, $string); However, I also want it to replace "binary_function([x1,y1])" with substring "X1Y1", and any length of the arguments inside the

regular expression checking valid hostname port [closed]

99封情书 提交于 2021-01-27 21:40:37
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Improve this question I've searched thru various websites and here for one that works but none do. I need a regex that validates a game server hostname is correct format. Example: gost.gflclan.com:27015 And also the normal ip:port 103.18.138.27:27015 The hostname version can also

R Ignore character within a Regex string

假如想象 提交于 2021-01-27 21:36:09
问题 I've looked all over for some regex that will cause R to disregard the next character within a regular expression string. For example, given myvector : myvector <- c("abcdef", "ghijkl", "mnopqrs") and a regex string: regexstring <- "[a-z]{3}XXXXXXXXX " which includes some unknown characters XXXXXXXXX, I want to tell R to ignore the final space in the regular expression string itself. After running the following, regexstring <- "[a-z]{3} " sub(regexstring, " ", myvector) gives, "abcdef"

Modulo operator in a regular expression

陌路散爱 提交于 2021-01-27 21:09:06
问题 I am attempting to write a regular expression to accept any binary strings with the only criteria being that the number of 0s is not a factor of 3 ([number of 0s] % 3 != 0). How can this be achieved? 回答1: You can use .match() to achieve this. .match() returns an array of all occurrences matching the regex. Using modulo on the returned array's .length will tell you if the number of 0s is divisible by 3. var someString = '012345167891abcd1efghi1jklmn'; var numOfOnes = someString.match(/(1)/g) /

Check for Numeric Value in Word Macro using Regular Expressions

…衆ロ難τιáo~ 提交于 2021-01-27 20:58:18
问题 Is it possible to directly use regular expressions in Word macros? I want to check if a string contains only numbers and the following code does not work. isNumber = RegEx.IsMatch("234", "[0-9]+$") I simply need it to return true or false. 回答1: You can create an instance of the VBScript.Regexp object within your code, but an easier solution would be to use the existing VBA function IsNumeric . Both methods are included below: Sub testNumeric() 'Use IsNumeric MsgBox (IsNumeric("12345"))