Newbie: Text Replacement in My Case

Java regex to replace in between text using a pattern

I would recommend using Files.lines() and Java Steam to modify the input. With your actual input you also don't need a regex:

try (Stream<String> lines = Files.lines(Paths.get("input.txt"))) {
String result = lines
.map(line -> line.replace("Images", "http://google.com/Images"))
.collect(Collectors.joining("\n"));
System.out.println(result);
}

If you really want to use a regex I would recommend to use a pattern outside the loop, because String.replaceAll() internally compiles the pattern every time you call it. So the performance is much better if you do not do Pattern.compile() for each line:

Pattern pattern = Pattern.compile("(href=\"javascript:openWin.*)(Images.*\")");
try (Stream<String> lines = Files.lines(Paths.get("input.txt"))) {
String result = lines
.map(pattern::matcher)
.map(matcher -> matcher.replaceAll("$1http://google.com/$2"))
.collect(Collectors.joining("\n"));
System.out.println(result);
}

Using this regex for replacement it will create two groups (between ()). You can use this groups in your replacement string by using $index. So $1 will insert the first group.

The result in both cases will be:

href="javascript:openWin(&#39;http://google.com/Images/DCRMBex_01B_ex01.jpg&#39;,480,640)"
href="javascript:openWin(&#39;http://google.com/Images/DCRMBex_01A_ex01.jpg&#39;,480,640)"
href="javascript:openWin(&#39;http://google.com/Images/DCRMBex_06A_ex06.jpg&#39;,480,640)"

Replacing text being appended to a class

Since the 'keydown' code is not in your control, you could try using the newer standard MutationObserver to detect DOM changes.

You can see an example with jQuery here: Can jQuery selectors be used with DOM mutation observers?

How to replace multiple substrings of a string?

Here is a short example that should do the trick with regular expressions:

import re

rep = {"condition1": "", "condition2": "text"} # define desired replacements here

# use these three lines to do the replacement
rep = dict((re.escape(k), v) for k, v in rep.iteritems())
#Python 3 renamed dict.iteritems to dict.items so use rep.items() for latest versions
pattern = re.compile("|".join(rep.keys()))
text = pattern.sub(lambda m: rep[re.escape(m.group(0))], text)

For example:

>>> pattern.sub(lambda m: rep[re.escape(m.group(0))], "(condition1) and --condition2--")
'() and --text--'

Replace only digit in a specific line of text file

try this :

$IPToFind='172.33.3.45'
$File=C:\myfile.txt

$Contentcsv=import-csv $File -Delimiter '|' -Header IP, ID
$Contentcsv | %{if($_.IP -eq $IPToFind){$_.ID=[int]$_.ID+1}; "{0}|{1}" -f $_.IP, $_.ID} | Out-File $File

If you want more explanation :

$IPToFind='172.33.3.45'

$Contentcsv=import-csv "C:\temp\test.txt" -Delimiter '|' -Header IP, ID
$Contentcsv | Foreach{

#modify value if IP is founded
if($_.IP -eq $IPToFind)
{
$_.ID=[int]$_.ID+1
};

#send tou output with -f (-format operator)
"{0}|{1}" -f $_.IP, $_.ID

} | Out-File "C:\temp\test.txt"


Related Topics



Leave a reply



Submit