Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting 'index' to find specific match from regex 1

Status
Not open for further replies.

Aneusomy

Programmer
Jan 15, 2001
20
0
0
CA
hi, :)
can anyone help me with the following problem.
i've designed a search engine, and would like to finish implementing highlighting of the search match results in another colour.

with a regex:
eg:
$teststr = "transferase protein is a ras molecule";
$thestr = "\bras\b";
if ($teststr =~ /($thestr)/) {
$matched = $1;
$wherematched = index($teststr, $matched);
}

in this case, i want the regex to match the word "ras" alone (not the unbound 'ras' in "transfeRASe"). This i have working.
however, to do the highlighting of the match result in the HTML output, i need to find out the position of the match made by regex.
the index function above does not go by the regex match position, but instead matches to the first occurrence of $1 ($1 = 'ras') and thus instead matches with "transferase" and gives an index of 7 (i want to get 25). Is there any way to 'link' the regex results with the index function?
I'm aware the index function allows to "skip" to the next match, but this is not what i want because in my case the match position is often variable (i.e. there may be other 'ras' mismatches before the real one that regex properly catches).
can anyone help?
THANKS!!!
Ben
 
Why not just use the regular expression to add your
highlighting? Something along the lines of
Code:
s/\bras\b/<FONT COLOR=&quot;#A00000&quot;>$1</FONT>/;
You could add a g on the end to match multiple occurences
on a line if you need that.
 
Bah. That should be $&amp; and not $1 in my little
code sample. Or enclose the pattern being matched
in parenthesis and $1 would work.
 
hi,
thanks for your response. however, still i need something that reports the position of the regex match and not a new match position made by the index function. do you know of anything that does this?
TIA,
ben
 
Hey, i'm pretty sure you don't want to be double-quote interpreting the
Code:
$thestr = &quot;\bras\b&quot;;
if you change that to single quotes, the whole thing works perfectly...
:)

&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Try this on for size.

The index of a substring is the same as the number of
characters preceding the substring. So this should
give you the number you are looking for:
Code:
$teststr = &quot;transferase protein is a ras molecule&quot;;
$thestr = &quot;\bras\b&quot;;
if ($teststr =~ /(.*)($thestr)/) {
       $matched = $2;
       $wherematched = length($1);
}
$wherematched should be the index of the first character
of the matched substring. In your example:
$1 = &quot;transferase protein is a &quot;
and length($1) = 25
 
Hi guys,
thanks for the suggestions. the reason i don't want to put in the \b boundaries is because i've left it so that the user can enter wildcard &quot;*&quot;'s (and i have to strip these out beforehand and replace with \b if they are before or after the keyword)... nevertheless i'm sure there is probably a better way to do it, but what i have now works.
i'll give the (.*) suggestion a try though, because it seems like it's the one !! thanks! :)
ben
 
Hi everyone,
in case anyone is interested, I've found another (more direct) solution to this problem. To report the exact position where a regex matches, the pos function does this.

$teststr = &quot;transferase protein is a ras molecule&quot;;
$thestr = &quot;\bras\b&quot;;
if ($teststr =~ /$thestr/) {
print &quot;match position = &quot;.pos($teststr).&quot;\n&quot;;
}

This is useful to know, because you can easily come up with examples where your regex match position cannot be retrieved by the function index.

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top