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

PHP - Using back references with file_exists in preg_replace

I am trying to replace content of some tags in included files with content from other files: $view = preg_replace('/{include ([[:alnum:]\.-]+)}/', ((file_exists('template/$1.html')) ? 'OK $1' : 'KO $1'),…
Bernard Rosset
  • 4,523
  • 6
  • 27
  • 29
0
votes
5 answers

How to group strings with whitespace, using sed?

Suppose my text file with the following strings: Apple foo foobar Banana foo foobar1 abc b c Orange barfoo Pear foo How do I group the strings that comes after Apple, Banana, Orange, and Pear? I could do this for Apple, but this wouldn't work for…
UberNate
  • 2,109
  • 2
  • 15
  • 15
0
votes
1 answer

.htaccess - Check if multiple files exists based on URL slug and rewrite accordingly?

From the following url: localhost/videos/the-best-games and a directory named "categories" with various files in it located in the same directory as .htaccess : ./games.txt ./sports.txt ./football.txt How to use the url slug "the-best-games" to…
RafaSashi
  • 16,483
  • 8
  • 84
  • 94
0
votes
2 answers

Sed script that will find a match in a line, and the change another part of that line

I'm trying to change the "FS" type of the lines containing c1t1 from ufs to ext2 and change c1t1 to c1t2 everywhere on those lines. This is what the file looks like: #device device mount FS fsck mount mount #to mount to fsck …
MeesterMarcus
  • 700
  • 1
  • 9
  • 25
0
votes
1 answer

Replace strings in a certain column with awk

I am trying to replace a string of numbers in the first column using awk, gsub and backreference. For instance, my input file is 1-00001 1 1-00001 1-00001-01 1 1-00001 1-00001-02 1 1-00001 and my desired output is 1-00001-00 1 1-00001 1-00001-01 1…
jamie
  • 99
  • 1
  • 1
  • 5
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
  • 17,781
  • 20
  • 98
  • 124
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
  • 441
  • 3
  • 11
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,197
  • 12
  • 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,256
  • 3
  • 16
  • 35
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
  • 364
  • 5
  • 10
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
  • 559
  • 4
  • 22
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
  • 401
  • 1
  • 3
  • 15
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
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
  • 13