What's the opposite of substr? - perl

I have a text file like so:
dave ran very quickly
dan very slowly ran
I am doing a regex to look for the word "ran" but I also need to know where it starts (in the first case it is character 6, in the second case it's 17).
I have (though it isn't much):
for(#lines){
if(/ran/){
# find where ran is so we can continue parsing
}
}

It's easy:
my $ran_pos = $-[0];
See the perlvar man page for a detailed description of the #- array.

I believe the index function is what you're looking for.
Here are a couple links:
http://perlmeme.org/howtos/perlfunc/index_function.html
http://www.misc-perl-info.com/perl-index.html

index STR,SUBSTR will return the position of a substring within a string.

Related

How to replace content within quotes via a file

Why I cannot use str_replace() to replace content between the ""? While I replace links within a file they get skipped since they are within quotes.
Example.
href="/path/to/file/is/here"
should be
href="/New/Path/To/File/Goes/Here"
If the paths/urls were not in quotes, str_replace() would work.
I'm assuming this is PHP. So, from the examples here:
http://php.net/manual/en/function.str-replace.php
You can see that you should not intercalate the same type of quotes.
So try changing the quotes in your code to single quotes or, change, the double quotes in your html to single quotes.
If that's not it, I hope at least that doc reference helps you.
This might help I usually code in java but php is pretty similar. Next time input part of your code so that the community can see your logic.
In your if statement on line 67 the 3rd variable $stringToSearch should be regex not the string your assigning it to. The purpose of regex as you know is to replace characters you don't want in your code as you already know
What you had that was not working:
// replacing string from files
//$stringToSearch = str_replace('"', "!!", $stringToSearch);
$stringToSearch = str_replace($toBeReplaced, $toBeReplacedWith, $stringToSearch);
//$stringToSearch = str_replace("!!", '"', $stringToSearch);
What I am thinking it should be:
$stringToRegex = str_replace('"', "!!", $stringToSearch);
$stringToSearch = str_replace($toBeReplaced, $toBeReplacedWith, $stringToRegex );
If anyone else has any suggestion it would be appreciated as i don't code in php.

How to take a substring with the endpoint being a carriage return and/or line feed?

How do I take a substring where I don't know the length of the thing I want, but I know that the end of it is a CR/LF?
I'm communicating with a server trying to extract some information. The start point of the substring is well defined, but the end point can be variable. In other scripting languages, I'd expect there to be a find() command, but I haven't found one in PowerShell yet. Most articles and SE questions refer to Get-Content, substring, and Select-String, with the intent to replace a CRLF rather than just find it.
The device I am communicating with has a telnet-like command structure. It starts out with it's model as a prompt. You can give it commands and it responds. I'm trying to grab the hostname from it. This is what a prompt, command, and response look like in a terminal:
TSS-752>hostname
Host Name: ThisIsMyHostname
TSS-752>
I want to extract the hostname. I came across IndexOf(), which seems to work like the find command I am looking for. ":" is a good start point, and then I want to truncate it to the next CRLF.
NOTE: I have made my code work to my satisfaction, but in the interest of not receiving anymore downvotes (3 at the time of this writing) or getting banned again, I will not post the solution, nor delete the question. Those are taboo here. Taking into account the requests for more info from the comments has only earned me downvotes, so I think I'm just stuck in the SO-Catch-22.
You could probably have found the first 20 examples in c# outlining this exact same approach, but here goes with PowerShell examples
If you want to find the index at which CR/LF occurs, use String.IndexOf():
PS C:\> " `r`n".IndexOf("`r`n")
2
Use it to calculate the length parameter argument for String.Substring():
$String = " This phrase starts at index 4 ends at some point`r`nand then there's more"
# Define the start index
$Offset = 4
# Find the index of the end marker
$CRLFIndex = $string.IndexOf("`r`n")
# Check that the end marker was actually found
if($CRLFIndex -eq -1){
throw "CRLF not found in string"
}
# Calculate length based on end marker index - start index
$Length = $CRLFIndex - $Offset
# Generate substring
$Substring = $String.Substring($Offset,$Length)

Substitute only one part of a string using perl

I have an array that have some symbols that I want to remove and even thought I find a solution, I will like to know if this is the right way because I'm afraid if I use it with array will remove the character that I might need on future arrays.
Here is an example item on my array:
$string1='22 | logging monitor informational';
so I try the following:
$string1=~ s/\s{6}\|(?=\s{6})//;
So my output is:
22 logging monitor informational
Is the other way that best match "|". I just want to remove the pipe character.
Thanks in advance
"I want to remove just the pipe character."
OK, then do this:
$string1 =~ s/\|//;
This will remove the first pipe character in the string. (You said in another comment that you don't want to remove any additional pipe characters.) If that's not what you want, then I'd suggest telling us exactly what you do want. We can't read minds, you know.
In the mean time, I'd also strongly recommend reading the Perl regular expressions tutorial.

SPH_MATCH_EXTENDED sphinx

I have the following in my index this is test, and I want to be able to search for "this test is".
In other words, I'm looking for exact match with no regards to the words position.
Is this true?
$cl->SetMatchMode( SPH_MATCH_EXTENDED);
$result = $cl->Query( '"this test is"~3', $index );
If it is true then is the number after the ~ should be the count of the words to get the exact match search with no regards to the positions?
I have tested the result and it looks good but I'm not sure.
Your use of the proximity operator looks fine to me.

replace string1 with string2 in many java files, only in comments

I have around 3000 instance of replacement done in hundreds of files. Replacing all occurance of string1 with string2 was easy. IntelliJ allows me to replace all occurences in "comments and strings".
The problem is that the same string appear in comments and real code. I would like restrict the replacement only in comment section ( we use mix of /**/ or // )
Any library/IDE/script that can do this?
use Regexp::Common 'comment';
...
s/($RE{comment}{'C++'})/(my $x = $1) =~ s#string1#string2#g; $x/ge;
Try using the following regex to find all comments, and then replace what you want afterwards:
/(?>\/\*[^\*\/]*\*\/|\/\/([^\n])*\n)/
The first part \/\*[^\*\/]*\*\/ Tries to find all /**/ pairs where it finds something that starts with /* and then contains something other than end tag */ and the contains end tag */.
THe other part checks something that starts with // and goes to endline(\n) and contains something not newline between ([^\n]*).
Thus it should all comments