backreference

Python regex - Replace bracketed text with contents of brackets

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-07 17:54:08
问题 I'm trying to write a Python function that replaces instances of text surrounded with curly braces with the contents of the braces, while leaving empty brace-pairs alone. For example: foo {} bar {baz} would become foo {} bar baz . The pattern that I've created to match this is {[^{}]+} , i.e. some text that doesn't contain curly braces (to prevent overlapping matches) surrounded by a set of curly braces. The obvious solution is to use re.sub with my pattern, and I've found that I can

regex: getting backreference to number, adding to it

只愿长相守 提交于 2021-02-04 18:13:27
问题 Simple regex question: I want to replace page numbers in a string with pagenumber + some number (say, 10). I figured I could capture a matched page number with a backreference, do an operation on it and use it as the replacement argument in re.sub . This works (just passing the value): def add_pages(x): return x re.sub("(?<=Page )(\d{2})",add_pages(r"\1") ,'here is Page 11 and here is Page 78\nthen there is Page 65',re.MULTILINE) Yielding, of course, 'here is Page 11 and here is Page 78\nthen

python : pass regex back-reference value to method

折月煮酒 提交于 2021-02-02 09:31:12
问题 I have this content: Data1 import filename.in Data2 and want to replace import filename.in line with the content of filename file, so I use this: content = re.sub(r'import\s+(.*)\s+\n', '\n' + read_file('\1') + '\n', content) read_file(in) returns the content of file in . def read_file(file): with open(file) as f: return f.read() the problem is that back-ref \1 does not eval to filename.in : No such file or directory: '\\1' any suggestion? 回答1: read_file(..) is called not by the re.sub . '\1'

Pass Java backreference to method parameter

泪湿孤枕 提交于 2021-01-20 09:42:16
问题 I need a Java port of this https://gist.github.com/jbroadway/2836900, which is basically a simple markdown regex parser in PHP. I was hoping I could use the backreferences, but I can't make it work. At the moment I'm not using a HashMap , I've got 2 JavaFX TextArea s where I'll get and set the text via a ChangeListener . { //... htmlTextArea.setText(markdownTextArea.getText() .replaceAll("(#+)(.*)", header("$0", "$1", "$2")); } private String header(String text, String char, String content) {

Seperate backreference followed by numeric literal in perl regex

馋奶兔 提交于 2020-12-20 08:00:50
问题 I found this related question : In perl, backreference in replacement text followed by numerical literal but it seems entirely different. I have a regex like this one s/([^0-9])([xy])/\1 1\2/g ^ whitespace here But that whitespace comes up in the substitution. How do I not get the whitespace in the substituted string without having perl confuse the backreference to \11 ? For eg. 15+x+y changes to 15+ 1x+ 1y . I want to get 15+1x+1y . 回答1: \1 is a regex atom that matches what the first capture

Seperate backreference followed by numeric literal in perl regex

人盡茶涼 提交于 2020-12-20 07:56:09
问题 I found this related question : In perl, backreference in replacement text followed by numerical literal but it seems entirely different. I have a regex like this one s/([^0-9])([xy])/\1 1\2/g ^ whitespace here But that whitespace comes up in the substitution. How do I not get the whitespace in the substituted string without having perl confuse the backreference to \11 ? For eg. 15+x+y changes to 15+ 1x+ 1y . I want to get 15+1x+1y . 回答1: \1 is a regex atom that matches what the first capture

Invalid reference \1 using sed when trying to print matching expression

為{幸葍}努か 提交于 2020-12-16 07:44:30
问题 Before I start, I already looked at this question, but it seems the solution was that they were not escaping the parentheses in their regex. I'm getting the same error, but I'm not grouping a regex. What I want to do is find all names/usernames in a lastlog file and return the UNs ONLY. What I have: s/^[a-z]+ |^[a-z]+[0-9]+/\1/p I've seen many solutions that show how to do it in awk, which is great for future reference, but I want to do it using sed. Edit for example input: dzhu pts/15

Invalid reference \1 using sed when trying to print matching expression

吃可爱长大的小学妹 提交于 2020-12-16 07:42:38
问题 Before I start, I already looked at this question, but it seems the solution was that they were not escaping the parentheses in their regex. I'm getting the same error, but I'm not grouping a regex. What I want to do is find all names/usernames in a lastlog file and return the UNs ONLY. What I have: s/^[a-z]+ |^[a-z]+[0-9]+/\1/p I've seen many solutions that show how to do it in awk, which is great for future reference, but I want to do it using sed. Edit for example input: dzhu pts/15

Invalid reference \1 using sed when trying to print matching expression

烂漫一生 提交于 2020-12-16 07:42:27
问题 Before I start, I already looked at this question, but it seems the solution was that they were not escaping the parentheses in their regex. I'm getting the same error, but I'm not grouping a regex. What I want to do is find all names/usernames in a lastlog file and return the UNs ONLY. What I have: s/^[a-z]+ |^[a-z]+[0-9]+/\1/p I've seen many solutions that show how to do it in awk, which is great for future reference, but I want to do it using sed. Edit for example input: dzhu pts/15