View Full Version : Reg Ex
bluewalrus
09-27-2009, 06:14 PM
Is there a way to do a search that keeps count like
x = 0;
RunaSearchfor(\d)numbers
x = x++;
$1 = x;
Replace with this
text$1aroundthenewvalue
djr33
09-28-2009, 12:52 AM
Could you just do that using a variable in a loop?
Maybe a real bit of code would make this clearer.
jscheuer1
09-28-2009, 01:01 AM
Depends upon the scripting language you are using, as to how it would be accomplished. Like in javascript, you cannot assign a value to $1 in a regular expression method that employs the $# convention for representing matches. However, via clever usage of the same feature, something like what you are describing could probably be done.
But you haven't made it very clear what exactly you are working with as to the source (like an array or an object, or perhaps just the text nodes of a given page, etc.) of the text strings to be modified or as to the language you intend to work in. It would also be good to tell us what you want done with these modified text strings once they are modified. Even more important may be, "Why do you want to do this?"
Clarify these issues and I'm sure that I or someone else can help out.
That said, if you already have the strings in an array, are using javascript, and want to replace each instance of a number in these strings with another number that increases sequentially, yes - that can be relatively easily accomplished.
I'm just not sure if that's really what you are going for. Remember too that a number may be like 1 or 9 or 0, ect. - single digit (\d in RegExp lingo, which would see something like 100 as three numbers), but could also be like 22 or 317 (\d+, which would also cover single digits, but without excluding or differentiating them from multiple digit numbers). Other expression can be constructed to deal with other contingencies as regards numbers, as far as the format in which they may be encountered in a string or set of strings. And consider that a given string in your array (or whatever collection of strings you have to work with) may have more than one instance each of whatever you consider to be a number. If so, do you want to address each number or just the first in each string?
bluewalrus
09-28-2009, 04:17 AM
Okay, I have a series of php and xhtml pages that have links to images. The image names have been renamed so now I need to re name the images starting from 1 and going through all the img links in the file. Like this
Search for: <a href="http://www.site.com/images/figure(\d).jpg"><img src="/images/images/figure(\d)tmb.jpg" alt="Figure (\d)" /></a>
First Replace would be: <a href="http://www.site.com/images/figure1.jpg"><img src="/images/images/figure1tmb.jpg" alt="Figure 1" /></a>
I'm running the regular expression in dreamweaver cs3 in the code view.
djr33
09-28-2009, 05:56 AM
So then all you need to do is loop through looking for the pattern of $text.$num.$text.$num..... etc.
Rename as needed, and you're done.
You don't even really need reg ex for this, if you wanted to do it the long way with strings, if for some reason reg ex gets too difficult.
You don't need to "count" using reg ex to get this done-- just find any cases with that pattern (text with any numbers) and replace the text around the numbers. It doesn't matter if you've got 1-100, or just 1, 4, 7, and 9... it'll all work the same way.
jscheuer1
09-28-2009, 05:58 AM
So what I think you are saying is that you want to use regular expression to edit a page or pages. Right? My editor can do that, but it is rather limited. Like I cannot have it automatically increment the replacement value. It sees everything as text, not as numeric values. Your editor might be as limited though. Using javascript one could probably get the page to output the incrementally changed values of its source to a textarea. Then one could copy that back to the actual source code. But PHP tokens (if any) would be resolved to their literal values and therefore need to be themselves replaced later, or possibly as part of the process. Another alternative would be a stream editor like sed used in a batch file that could increment the replacement values. That way the rest of the source would be preserved as is.
But before we go down either of these roads, is this what you are trying to do or not? And does your editor allow you to increment replacement values numerically?
Regardless, unless all of the values are single digits, the simple \d (or [0-9] in sed) will not do.
bluewalrus
09-30-2009, 04:01 AM
Yes, I want to edit multiple pages with this regular expression (one page at a time though). With the \d I've been able to seperate text and numerical values. They should in almost all instances be single digits (haven't found one that exceeded 9). So the single should work fine and I can just alter the few instances where it exceeds that by hand. I don't know of a way to increment the replacement values.
In dreamweaver I have the following options when regular expression is checked:
match case, find next, find all, replace, replace all, close, help,
Search in: text, source code, text advanced, specific tag
Find in: current document, selected text, open documents, folder, selected files in site, entire current local site.
jscheuer1
09-30-2009, 08:20 AM
I'm still unclear as to precisely what you want to do. When using regular expressions, it is best to be as specific as possible. And unfortunately I'm not at all familiar with how Dreamweaver does RegExp. My editor takes a slightly different approach than sed, which are both different than javascript. The commonalities far outweigh the differences in both cases but there are important differences that can trip you up if you are not aware of them.
Something you could try is a fairly simple script:
<script type="text/javascript">
function regExBod(){
var b = document.body.innerHTML, t, i = 0, r = new RegExp('(src="\\D+)\\d(\\.jpg)', 'g'),
m = b.match(r);
if(m){
for(i; i < Math.min(10, m.length); ++i){
b = b.replace(m[i], m[i].replace(r, '$1' + i + '$2'));
}
t = document.createElement('textarea');
t.style.position = 'absolute';
t.style.top = t.style.left = 0;
t.style.width = t.style.height = '300px';
t.value = b;
document.body.appendChild(t);
t.focus();
t.select();
}
}
</script>
If you put that on your page (in the head) and run it by typing this into the address bar:
javascript:void(regExBod())
and hitting enter, it will output the the HTML of the body altered by the regular expression and javascript loop to a textarea that will pop up over the page. You could then copy this altered text back to the page. Be sure to make a backup first!
This particular regex combined with the javascript loop will find anything that matches the pattern:
src="some_stuff_that's_not_numbers_followed_by_one_number_a_dot_and_jpg
It will save the red stuff and replace the number with 'i', which is incremented from 0 to 9 as it goes along. Once it gets to 9 or stops finding matches, it will stop looping. You may need to tweak either the regular expression and/or the script code to get just what you are after. I just ran this against a page, it performed as expected and gave no error.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.