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('http://google.com/Images/DCRMBex_01B_ex01.jpg',480,640)"
href="javascript:openWin('http://google.com/Images/DCRMBex_01A_ex01.jpg',480,640)"
href="javascript:openWin('http://google.com/Images/DCRMBex_06A_ex06.jpg',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
Net::Ssh with Non Unix/Linux Host
Ruby Check Whether Program Is Currently Being Closed
Understanding Ruby Method Parameters Syntax
Why Won't Ternary Operator Work with Redirect
Cannot Require 'Nokogiri' in Rails (But Works in Irb)
Pageobject with Ruby - Set Text in a Text Field Only Works in the Main File
Custom Gem Execution Fails with Nomethoderror
How to Escape All Characters with Special Meaning in Regex
On Destroying Session via Devise "Couldn't Find User with 'Id'=Sign_Out"
Rspec: How to Write Unit Test Case to Receive an Exception Which Is Getting Raised in Private Method
Ruby Devise, Sessionscontroller.Create, JSON - Getting Nameerror: Undefined 'Build_Resource'
Render Erb from Database into View Problem Please Help!
Rails 3 - Has_And_Belongs_To_Many
What Ruby Features Are Used in Chef Recipes
Local Server Error After Upgrading Ruby from 1.8.7 to 1.9.2 (With Rails 3.1.1)