regex

VSCode Regex Find/Replace In Files: can't get a numbered capturing group followed by numbers to work out

♀尐吖头ヾ 提交于 2021-01-27 18:57:44
问题 I have a need to replace this: fixed variable 123 with this: fixed variable 234 In VSCode this matches fine: fixed(.*)123 I can't find any way to make it put the capture in the output if a number follows: fixed$1234 fixed${1}234 But the find replace window just looks like this: I read that VSCode uses rust flavoured rexes.. Here indicates ${1}234 should work, but VSCode just puts it in the output.. Tried named capture in a style according to here fixed(?P<n>.*)123 //"invalid regular

How can I customize what characters are filtered out using string.punctuation?

☆樱花仙子☆ 提交于 2021-01-27 18:52:03
问题 I have a string with which I would like to remove all punctuation. I currently use: import string translator = str.maketrans('','', string.punctuation) name = name.translate(translator) However, for strings which are names this removed the hyphen also, which I would like to keep in the string. For Instance '\Fred-Daniels!" Should become "Fred-Daniels". How can I modify the above code to achieve this? 回答1: If you'd like to exclude some punctuation characters from string.puncation , you can

C++ Regex to match '+' quantifier

假装没事ソ 提交于 2021-01-27 18:33:57
问题 I want to match expression of the pattern a space followed by a (addition operator or subtraction operator) For example: " +" should return True I have tried the using std::regex_match on the following regular exp: " [+-]" , "\\s[+-]" , "\\s[+\\-]" , "\\s[\\+-]" but they all return false. What should be the correct expression ? EDIT Here's the test code: #include<iostream> #include<string> #include<regex> using std::cout; int main() { std::string input; std::cin>>input; const std::regex ex("

How do capture groups work? (wrt python regular expressions)

微笑、不失礼 提交于 2021-01-27 18:19:53
问题 While using regex to help solve a problem in the Python Challenge, I came across some behaviour that confused me. from here: (...) Matches whatever regular expression is inside the parentheses. and '+' Causes the resulting RE to match 1 or more repetitions of the preceding RE. So this makes sense: >>>import re >>>re.findall(r"(\d+)", "1111112") ['1111112'] But this doesn't: >>> re.findall(r"(\d)+", "1111112") ['2'] I realise that findall returns only groups when groups are present in the

Negative lookahead in regex to exclude percentage (%) in R

99封情书 提交于 2021-01-27 18:00:30
问题 I wish to extract numbers with any decimals (at least one number both sides of the decimal), but not patterns followed by percentages. Therefore, I believe I need a negative lookahead (so it can see if the number is followed by a percentage sign). For clarity, I would want to extract "123.123" , but would not like to extract "123.123%" I have tried a dozen syntax arrangements but cannot find the one that works. This successfully extracts the decimal pattern. c("123.123%", "123.123") %>% str

Regex optional suffix

时光总嘲笑我的痴心妄想 提交于 2021-01-27 17:55:41
问题 I'm using the following regular expression to validate CSS sizes: ([0-9]*\.?[0-9]+)(em|px|%) The following sizes are therefore valid: 10px 10.2px 1.5em 100% How can I change the regex to make the unit (em|px|%) optional to allow a number only? 回答1: You can simply add ? at the end to make the unit optional. ([0-9]*\.?[0-9]+)(em|px|%)? As the unit is now optional, this pattern will allow numbers only entities as well. Be aware however, that doing some will also partial match the content making

Can we put regular expression for a value in a DTD?

拥有回忆 提交于 2021-01-27 17:52:18
问题 I would like to know if there is a way to associate regular expression for the value of an attribute. like in XML Schema: <xs:pattern value="([0-9]|[A-Z]){3}" /> to make the DTD match with this XML line: <airport iata="LAE" name="Nadzab Airport" city="Nadzab" country="Papua New Guinea"> 回答1: No, DTDs do not support regex. Use XSD for vastly superior datatyping. DTD attribute values can be enumerations, however: <!ELEMENT airport EMPTY> <!ATTLIST airport iata (LAE|LAX|LGA) #IMPLIED> 来源: https:

Is there a way to remove everything except characters, numbers and '-' from a string

你说的曾经没有我的故事 提交于 2021-01-27 16:43:48
问题 I am really bad with regex but here is what i am trying to acheive StringOne = [5, -, e, 4, e, e, 0, 5, 3, 5, e, b, e, e, 5, 0, a, 4, 3, 3, 1, 9, 0, 8, 1, b, 3, 6, 1, b, 3, 6, 4, d, 3, 3, -, 2, 0, c, c, 1, c, 1, -, ., 8, 3, -, 4, 8, 4, 3]; And I want to remove everything but numbers, characters and '-' I found an answer to save characters and number by doing this StringOne = StringOne.replaceAll("[^a-zA-Z0-9]", ""); But I also want to save '-' Is there some way I can add that to the regex or

Regex for The Same Pattern Multiple Times in One Line

好久不见. 提交于 2021-01-27 16:25:32
问题 The pattern I'm looking for is this: TXT.*\.txt That pattern can occur multiple times in any given line. I would like to either extract each instance of the pattern out or alternatively delete the text that surrounds each instance using sed (or anything, really). Thanks! 回答1: You can use grep as: grep -o 'TXT[^.]*\.txt' file 回答2: You can use Perl as: $ cat file foo TXT1.txt bar TXT2.txt baz foo TXT3.txt bar TXT4.txt baz $ perl -ne 'print "$1\n" while(/(TXT.*?\.txt)/g)' file TXT1.txt TXT2.txt

re.findall() where I want all unique instances of the regex on the page

最后都变了- 提交于 2021-01-27 16:21:18
问题 As the title suggests, I want to run code like this (top_url_list is just a list of urls I'm looping through to find instances of these filename conventions that I'm looking for with regex: name_files = [] for i in top_url_list: result = re.findall("\/([a-z]+[0-9][0-9]\W[a-z]+)", str(urlopen(i).read())) Where the objective is to grab all of the instances where the regex checks out, hence the 'findall()" function. The problem is, it's important that I only get distinct/uniques of each instance