Questions tagged [backreference]

Back references are regular expression constructs that make use of capturing in regex to perform replacement based on parts of the matched string captured during a match sequence in the regexp pattern.

Back references and s are regular expression constructs that makes use of capturing in to perform matching and replacement that remembers parts of the matched string during a match sequence in the regexp pattern.

The back-referencing constructs present in all engines are \1 to \9, where \1 back-reference references the first ( ) capturing group.

Read more:

392 questions
0
votes
1 answer

Concrete5.7 Stacks - PHP Maximum execution time exceeded

We are having an odd issue with Stacks in Concrete5.7: we are starting to collect quite a few stacks (64 currently) and our server has started throwing server errors (PHP Fatal error: Maximum execution time of 30 seconds exceeded in…
Matt Rice
  • 646
  • 5
  • 18
0
votes
1 answer

Capture repeating, comma-separated strings in regex

I have a requirement to capture multiple phrases available in a line (or a input string). Let's say input is = "this is description,this is description" So I need to detect the string "this is description" has repeated twice in the input. This is…
greatb
  • 5
  • 1
0
votes
1 answer

REGEX - specify groups, grouping order

When I write regex as follows: /(.*)/(.*) from string /aaa/bbb/ccc I get groups: #1 aaa/bbb #2 ccc But I would like it to be like: #1 aaa #2 bbb/ccc How can I change grouping order?
michalsol
  • 752
  • 8
  • 29
0
votes
1 answer

Regex backreference not working

I want to match this html-like pattern: <12>Some content with \n in it<12> Important is that only complete items are marked (numbers MUST match), means when one tag is missing the content should not be marked. <12>Some content with \n in…
bademeister
  • 107
  • 1
  • 11
0
votes
1 answer

R: Get position of first backreference in regex match

I'm using R. I have a regular expression with a capturing group. I'd like to find the position and length of the first backreference (\\1) in the first matching substring. For example, let's say the regex is b(a+)b and the string is bbaaab; the…
wlad
  • 2,073
  • 2
  • 18
  • 29
0
votes
0 answers

Why does sed have different behavior depending on input method?

On Mac OS 10.9, I have a file, test.txt: _static _images running the command cat test.txt | sed -E -e "s/(_static|_images)/foo\1/g" has the expected output of foo_static foo_images but running the command to edit the file in place: sed -i -E -e…
thewiglaf
  • 375
  • 1
  • 4
  • 11
0
votes
1 answer

PHP preg_replace escape ampersand in backreference regex

Ok, I've checked and looked, but today my SearchFU is not strong. I am encountering an error with backreferences in some Regex preg_replace('~$\s*
([^<]*?)
$\s*~me', '\1', $source); I could really use a hand with. The expression finds
Toaster
  • 93
  • 1
  • 7
0
votes
1 answer

How to create multiline python regex to parse go imports

I'm trying to parse the following with a python regex. import ( "github.com/user/qrt" "fmt" "github.com/user/zyx" ) import "abcdef" import "abzdef" Ideally a single regex would yield: everything inside the parens as a single group…
bdbaddog
  • 3,357
  • 2
  • 22
  • 33
0
votes
3 answers

js - backreference not working as expected

I have a string "styles-123.min.js", that I want to replace with "styles-456.min.js". So I do this: str = "styles-123.min.js"; str = str.replace(/(styles|apps)/, "$0" + "-456"); alert(str) The $0 isn't parsed and the result is "$0-456-min.js".…
Mister_L
  • 2,469
  • 6
  • 30
  • 64
0
votes
2 answers

Using backreference wit php preg_match_all

I'am quite new in regex and php but I'm facing an issue I can't handle alone. I've prepared this regex to find patterns starting with upper-case letter. It could sounds something like : capture any pattern that starts with one or more Upper-case…
Charles
  • 9
  • 1
0
votes
1 answer

Adding backreferenced value to its replacement

I am trying to add a number from a backreference to another number, but I seem to get only concatenation: textStr = "" new_str = textStr.gsub(/(testsuite…
amadain
  • 2,724
  • 4
  • 37
  • 58
0
votes
1 answer

Regex calling backreferences from all group interations

I'm catching international numbers and running a regex to replace the characters people like to put between numbers. I'm using the below RegEx: [+]([0-9]{1,3})(([\s\-\.\(\)]*)([0-9]*)([\s\-\.\(\)]*)){1,3} It works great but when I use a repeated…
Err
  • 890
  • 2
  • 11
  • 32
0
votes
1 answer

Backreference search replace with trailing numeral

I'm using vim to find a four digit number and replace the last digit, but unlike new school sed which uses the ${1} value for a back reference, it uses \1 and I'm struggling to append the following number. I need to do: a=1234 => a=1235 b=1374 =>…
rupert160
  • 1,441
  • 1
  • 17
  • 19
0
votes
1 answer

PHP regex backreference glitch

Wanting to parse custom moderator's tags in mybb posts. [mod="Nomad"]Curabitur eu ultricies nunc[/mod]. Donec et luctus nisi, a imperdiet ipsum. [mod="Nomad"]Morbi feugiat tellus lectus, a auctor ligula elementum ac[/mod]. The following php…
Nomad
  • 58
  • 8
0
votes
1 answer

Need some help understanding backreferences in ruby

I was working on a coderbyte problem where I output the number of occurrences of a character along with the corresponding character. For example "wwwggopp" would return 3w2g1o2p. I was able to solve it but I compared my answer to someone else's…