I want to add a new String NEWSTRING under (next line) the String EXISTINGSTRING which is present in multiple JSP files in my project. I am able to search EXISTINGSTRING using a regular expression. Is there any way to add NEWSTRING under EXISTINGSTRING in all JSP files using replace functionality or some other way? I am using IBM RAD to do this search.
It can be done with replace.
When searching for EXISTINGSTRING make this line a group
pattern = "^(.*EXISTINGSTRING.*)$" // this will identify line with searched string
than replace it with group1 + NewLine + NEWSTRING
replaceWith = "$1\nNEWSTRING"
Related
I've seen a bunch of examples on this online but I can't seem to find the one I'm looking for which uses Regex. I've seen many that use a loop and use a lot of lines of code but I'd like to see an example of Regex.
What I'm trying to create is an app that will connect too a webpage take the source search for a keyword once its found copy the text from that keyword to another keyword and save it into a string or to a textbox whatever.
I'm already using web request to get the information and put it into a string I just need to search the string for what I am looking for.
The reason for this app is to search webpage for an updated version of some software I'm using. I want to monitor for updates and the app to notify me when an update is available. Just a simple app but having issues searching for what I need.
For Example:
first words to search for: Server 64-bit
second words/characters to search for: </div>
grab first words everything in between and last word saved into a string.
EDIT: The information I am trying to grab is this....
Server 64-bit
<span class="version">
3.0.13.6
</span>
</h3>
<div class="checksum">SHA256: c7eeb1937b0bce0b99e7c7e20de030a4b71adcaf09750481801cfa361433522f</div>
you can use the following code with RegEx to return the whole sentence including the two keywords you are providing
Dim str As String = "first words to search for: Server 64-bit second words/characters to search for: </div>"
str = str.Replace(vbNewLine,"|")
Dim strA As String = Regex.Match(str, "Server 64-bit(.*?)</div>", RegexOptions.Singleline).Value
Msgbox(strA)
Or you can use the following expression to get only value between this two keywords:
Dim strA As String = Regex.Match(str, "(?<=Server 64-bit)(.*)(?=</div>)", RegexOptions.Singleline).Value
Maybe not the prettiest solution, but i would save it into a string. Then iterate through it with the string.contain("Server 64-Bit") and then split the whole thing and then split the remaining part of the string at the next and retrieve only the first part.
Dim Information As String
Dim Splitstring As String
If Information.Contains("Server 64-Bit") Then
Dim parts As String() = Information.Split("Server 64-Bit")
For Each part In parts
SplitString As String = part(1)
Next
If SplitString.Contains("</div>") then
Dim parts As String() = Information.Split("</div>")
For Each part In parts
Dim ResultString As String = part(0)
'Displaying Result in a MsgBox
MsgBox(ResultString)
Next
End If
End If
Im currently only at my Phone, so I cant actually test this, but this should work.
I have a list like this
"Boring makes sense!"
"http://www.someurl.com/listsolo.php?username=fgt&id=46229&code="
"http://www.someurl2.com/members/listearn.php?username=mprogram&id=465301"
"All is there?"
"http://www.someurl.com/listsolo.php?username=loopa&id=46228&code="
"http://www.someurl3.com/members/mem.php?&mprogram"
"http://someurl4.com/members/mem.php?&loop"
I need to remove any kind of text on particular line including double quots with RegEx in vb.net
Dim fileName As String = "C:\Downloads\Links.txt"
Dim sr As New StreamReader(fileName)
While Not sr.EndOfStream
Dim re As String = sr.ReadLine()
If Not re.StartsWith("http") Then
re = Regex.Replace(re, "(^[A-Za-z]+)", "", RegexOptions.Multiline)
lblTest.Text += re.ToString()
End if
End While
sr.Close()
How to do it ...in simple way?
Using Linq, reading from file, filtering and re-writing back to it :
File.WriteAllLines("some path", From line In File.ReadAllLines("some path")
Where line.StartsWith("http"))
I figured it out :-), this regex
.[A-Za-z]\w+ .*
remove whole line of text with double quotas. I test regex here. Anyway, thanks for help.
I have this string for example: "Example_string.xml"
and i would like to add before the "." _DateTime of now so it will be like:
"Example_string_20151808185631.xml"
How can i achieve it? regex?
Yes, you can achieve that through the use of a look ahead. For instance:
Dim result As String = Regex.Replace("Example_string.xml", "(?=\.)", "_20151808185631")
Since the pattern only matches a position in the string (the position just before the period), rather than matching a portion of the text, the replace method doesn't actually replace any of the input text. It effectively just inserts the replacement text into that position in the string.
Alternatively, if you find that confusing, you could just match the period and then just include the period in the replacement text:
Dim result As String = Regex.Replace("Example_string.xml", "\.", "_20151808185631.")
If you don't want to just look for any period, and you want to be more safe about it (such as handling file names that contain multiple periods, then instead of \., you could use something like \.\w+$. However, if you need to make it that resilient, and it doesn't have to be done with RegEx, it would be better to use the Path.GetFileNameWithoutExtension and Path.GetExtension methods, as recommended by Crowcoder. For instance, you may also need to make it handle file names that have no extension, which even further complicates it.
or...
Path.GetFileNameWithoutExtension("Example_string.xml") + "_20151808185631" + Path.GetExtension("Example_string.xml")
How about:
Dim sFile As String = "Example_string.xml"
Dim sResult As String = sFile.ToLower.Replace(".xml", "_" & Format(Now(), "yyyyMMddHHmmss") & ".xml")
MsgBox(sresult, , sFile)
I have the following string in VBScript:
myPath = "C:\Movies\12 Monkeys\12_MONKEYS.ISO"
The path C:\Movies\ is always going to be the same. So here is another path as an example:
myPath = "C:\Movies\The Avengers\DISC_1.ISO"
My question is, how can I pull only the movie folder name, so in the above examples I would get:
myMovie = "12 Monkeys"
myMovie = "The Avengers"
Is there a way to use RegEx with this? Or should I just do some substring and index calls? What is the easiest way to do this?
Consider the code below:
arrPathParts = Split(myPath, "\");
myMovie = arrPathParts(2);
Split the string where the delimiter is the backslash character. Splitting a string returns an array of strings. Your movie is the third item in the array of strings.
http://regexr.com?3332n
(?<=C:\\Movies\\).*?(?=\\)
You use assertions so that it finds a string that starts with C:\Movies but does not include it in the results, then a greedy operator to find everything up until the forward slash. You use a look ahead assertion to exclude the forward slash from the results.
I am having a regex replace issue that i can't seem to figure out for replacing some configured parameter for a file path.
Here is what I have so far:
The regex for a filepath may not be perfect but it seems to work ok.
regex: ^(?<path>[^\\/*?<>|]+)\\\\(?<filename>.+)\\.(?<ext>.mp4$)
file name match results name: $2
So what this is doing is searching a listing of files where the extension is mp4 and using the configured match result, it will return that as a "file name".
Target string examples,
\\\\folder\music\hello.mp4
result filename = "hello"
What I would like to do is be able to take either the results from a regex match and be able to replace the name of the file/extension/path by a configured setting.
So If someone wanted for all the matched results to replace the file name with "goodbye", how would i accomplish this. This is what i have now.
std::string sz_regex_pattern("^(?<path>[^\/*?<>|]+)\\(?<filename>.+)\.(?<ext>.mp4$)");
boost::cmatch rm;
boost::regex pattern(sz_regex_pattern, regex::icase|regex_constants::perl);
std::string complete_file_name_path = "\\folder\music\hello.mp4";
bool result = boost::regex_match(complete_file_name_path , rm, pattern);
std::string old_filename= rm.format("$2"); // returns the name of the file only
What appears to work but limits it to a filename where the folder is not the same name so,
\\folder\music\hello\hello.mp4 would have issues with the regex_replace below.
std::string new_filename = "goodbye";
std::string sz_new_file_name_path = boost::regex_replace(complete_file_name_path, old_filename, new_filename);
so i can later,
boost::filesystem::rename(complete_file_name_path, sz_new_file_name_path);
Any help would be appreciated.
Find and replace is completely unnecessary because you already have all of the components you need to build the new path.
REPLACE
std::string sz_new_file_name_path = boost::regex_replace(complete_file_name_path, old_filename, new_filename);
WITH
// path + newFileName + ext
std::string sz_new_file_name_path = rm.format("$1") + "\\" + new_filename + "." + rm.format("$3")
You could probably split out the components to see what you have with:
^(.*?)\\?([^\\]+)\.([a-zA-Z0-9]+)$
edit or even less specific ^(.*?)\\?([^\\]+)\.([^.]+)$ non-validating
$1 = path
$2 = filename
$3 = extension
The separator between path, filename and extension are not captured.
With this information you could construct your own new string.
If you want to specifically search for say mp4's something like this would work:
^(.*?)\\?([^\\]+)\.mp4$