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:

357 questions
0
votes
1 answer

Sublime Text 2 regex matching with backreference

I have a multi-line string Some testStringHere I am using this regex pattern to find it (Some\s.*)(.|\n)* & replace it with \1\2 Instead of getting the same text back, I get Some test e Why isn't the second backreference working? Is there a…
user
  • 15,863
  • 15
  • 90
  • 110
0
votes
1 answer

Nginx Regex and optional backreference

I want to optionally match a string (asdf) and remove it from a rewrite. location ~ /somefolder { rewrite ^/somefolder(.*)(?:asdf)?(.*html)$ http://example.com$1$2 permanent; } So this would rewrite the requested url to the root domain as well…
mhumesf
  • 421
  • 2
  • 10
0
votes
2 answers

Sub-pattern in regex can't be dereferenced?

I have following Perl script to extract numbers from a log. It seems that the non-capturing group with ?: isn't working when I define the sub-pattern in a variable. It's only working when I leave out the grouping in either the regex-pattern or the…
EverythingRightPlace
  • 1,189
  • 11
  • 33
0
votes
0 answers

use str_replace with backreference

I want to replace many different things with many other different things in a forum post. I have an Array with all things to be replaced: $stuff = array("word", "sentence", "cat"); Actually i want to take the string to be replaced to create the…
T_01
  • 1,114
  • 3
  • 15
  • 28
0
votes
0 answers

Eager loading backreferences in ActiveRecord in Rails

Apparently in my Rails application ActiveRecord is running more SQL queries than I would like. I have a simple one-to-many relation. For each package… class Package < ActiveRecord::Base has_many :screenshots end …there are several…
0
votes
1 answer

Notepad++ v6.3.2 backreference regex replace not working

I have seen the instructions on using $1 in order to backreference the replace, but it is not working for me. Example: I search for <header to replace with $1 class="bold" and instead of <header class="bold" I get $1 class="bold" Am I…
RKichenama
  • 324
  • 3
  • 9
0
votes
1 answer

.htaccess comparing with API_VERSION

I'm struggling with a htaccess file. What I need to do, is do a rewrite rule when the apache version is lower then a certain version, else a different rule. This is about backreferences that were introduced in apache 2.2.7, but I still need to…
Nico
  • 509
  • 3
  • 18
0
votes
0 answers

Backreference in regular expression doesn't work

I am using this simple code: if (data.matches("(\\d+)\\D+")) data = "$1"; I want to find a string that begins with a number and then have non-number string. I need to extract the number itself. The backreference doesn't work. I am getting that…
AJ Gottes
  • 359
  • 1
  • 2
  • 11
0
votes
2 answers

SQLAlchemy: a combined backref on multiple join paths

Say we have a 'People' table that contains 'HomeAddressID' and 'WorkAddressID' columns. We are defining a multiple join paths relationship to table 'Addresses' like this: HomeAddress = relationship('Addresses',…
0
votes
1 answer

PHP: str_pad and backreference

How do I use str_pad with a backreference? The way I'm using it, it reads the characters between the single quotes as characters rather than the backreferenced string. Escaping the single quotes returns an error. $y = 'CHG1234567'; $y =…
user2001487
  • 313
  • 3
  • 12
0
votes
2 answers

How can I use a javascript regex backreference in a function?

var string = input.replace(/\[noparse\]([^\]]+)?\[\/noparse\]/ig, ''+removeBrackets('$1')+''); This expression should be taking a string and encoding the parts wrapped in [noparse] tags so they don't render in a textarea. I…
user1755043
  • 281
  • 1
  • 12
0
votes
1 answer

makefile backreference to matched text

Suppose, I have 2 files, dependent upon each other: ./pictures/1_data.tex | V ./data/1.pl So, 1_data.tex is generated from the Perl file. To do it I have the following rule in the makefile: ./pictures/1_data.tex:…
user4035
  • 19,332
  • 7
  • 51
  • 78
0
votes
3 answers

Parsing of a string with the length specified within the string

Example data: 029Extract this specific string. Do not capture anything else. In the example above, I would like to capture the first n characters immediately after the 3 digit entry which defines the value of n. I.E. the 29 characters "Extract…
Tim Radcliffe
  • 1,853
  • 1
  • 19
  • 28
0
votes
3 answers

Vim regex, backreference to `[^s]` matches

In a file with something like: self.phrase self.phrases self.phrase.lower() self.phrase_lowered self.phrases[self.phrase] I'd like to replace all self.phrase with self._phrase, except for self.phrases to…
Kache
  • 11,723
  • 10
  • 47
  • 71
0
votes
3 answers

Accessing $1 etc. outside of string.replace() in javascript regex?

As we all know $1 and so on are backreferences to captured groups in a string.replace() when using a regex, so you can do something like: string.replace(/(http:\/\/\S*)/g, 'link<\/a>') Now my question is whether there…
Wingblade
  • 8,065
  • 9
  • 30
  • 41