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
3
votes
2 answers

Perl's unpack("A4/A*") length+bytes syntax in regular expression form

As detailed in the perlpacktut, you can use an unpack string of X/Y* to first get the length of a byte stream and then read exactly that many bytes. However, I'm struggling to find anything like that within a regular expression with, say, plain…
SineSwiper
  • 2,046
  • 2
  • 18
  • 22
3
votes
2 answers

Vim regex: overwritten back references?

Project: Take Wikipedia's list of Roman consuls, put the data in a CSV so I can make a graph of the rise and fall of various gens in terms of consulage Example data source: 509,L. Iunius Brutus,L. Tarquinius Collatinus suff.,Sp. Lucretius…
Calpau
  • 921
  • 10
  • 21
3
votes
3 answers

Back-reference when preprend using sed linux command

I'm trying to prepend the first character of "monkey" using this command: echo monkey | sed -E '/(.)onkey/i \1' But when I use it like this, the output shows 1 monkey I actually hope to see: m monkey But back-reference doesn't work. Please…
3
votes
1 answer

greedy backreference in python's reguar expression?

In my case, I want to capture repeated characters in text; at the same time, at most 3 characters before and behind the repeated patterns should be captured too. For…
oyster
  • 537
  • 3
  • 15
3
votes
1 answer

powershell cannot use methods on backreference matches

i tried harder to convert the case on the fly but it seems that powershell methods does not run for backrefence matches example below: $a="string" [regex]::replace( "$a",'(.)(.)',"$('$1'.toupper())$('$2'.tolower())" ) > string $a -replace…
3
votes
1 answer

Performing a second replace in backreference for a regexp

I have lines from a web page of the form description with spaces which I want to convert to a csv format "url%20%with%20spaces","description with spaces" to feed into a mediawiki page that expects external links to…
vicarage
  • 83
  • 1
  • 6
3
votes
1 answer

Java Regex - capture string with single dollar, but not when it has two successive ones

I posted this question earlier. But that wasn't quite the end of it. All the rules that applied there still apply. So the strings: "%ABC%" would yield ABC as a result (capture stuff between percent signs) as would "$ABC." (capture stuff after $,…
JGFMK
  • 8,425
  • 4
  • 58
  • 92
3
votes
4 answers

How to find double letters and replace them with triple letters?

I'm not using any particular coding language, simply a program with "find" and "replace" where both fields use Regex. For example, the phrase too many professionals would turn into tooo many professsionals I want to "find" any occurrences of…
3
votes
1 answer

Javascript regexp: Using variables in backreference pattern?

I've got a pattern to find matches in a querystring: 'url.com/foo/bar?this=that&thing=another'.replace(/(thing=)([^&]*)/, '$1test') What I'd like to be able to do is use variable values as the param to match…
3
votes
2 answers

R: regex to capture all instances after a given character

Given string ab cd ; ef gh ij, how do I remove all spaces after the first space after ;, i.e. ab cd ; efghij? I tried using \K but can't get it to work completely. test = 'ab cd ; ef gh ij' gsub('(?<=; )[^ ]+\\K +','',test,perl=T) # "ab cd ;…
dasf
  • 1,035
  • 9
  • 16
3
votes
2 answers

Get backreference name-based regex from index-based regex

I have this regex string: ^(\()?\d+(?(1)\))$ It will match following strings: 123 (1234) 1 (33) But will not match following strings: (123 123) I would like to convert the index-based regex to name-based regex, but following regex seem don't…
NoName
  • 7,940
  • 13
  • 56
  • 108
3
votes
1 answer

How can I do multiple replace using a shared backreference?

I have a need to do some data-transformation for data load compatibility. The nested key:value pairs need to be flattened and have their group id prepended to each piece of child data. I've been trying to understand the page at Repeating a…
rumpled
  • 43
  • 5
3
votes
2 answers

Using python regex with backreference matches

I have a doubt about regex with backreference. I need to match strings, I try this regex (\w)\1{1,} to capture repeated values of my string, but this regex only capture consecutive repeated strings; I'm stuck to improve my regex to capture all…
Jess
  • 53
  • 1
  • 1
  • 5
3
votes
4 answers

How to pass Javascript back reference to key or function?