How to change a value between two characters SQL - mysql

This question is divided into two parts. I will start with the first and obvious to the title:
On the frontend I save this value:
Hello you are: HERE The page you can visite is www.test.se
In the db i am trying to write a script that will change the value between ":" and "The"
Although the value that is stored is a bit diffirent in the db...there it looks like this:
"Hello you are: <br/> <br/> <br/> HERE <br/><br/><br/><br/>The page
you can visite is" www.test.se
I want to change "HERE" to "XXXX"
My attempt so far:
I found these two soloutions that are simillar to my question
Soloution One
Soloution Two
Sadly i could not get this to work...I attempted this:
select SUBSTRING(
body,
CHARINDEX(':', body) +1 ,
CHARINDEX('The',body))
from #temp
I read up on substring and i am aware of that the last paramater is how long from the start position it should go but i want it to stopp at "The" and dont really know how to.
Second part of this question:
I only want to change the value of "HERE" how can i do that and avoid deleting the br taggs?
EDIT:
The value that i am trying to change is not always the same so a simple update statment will not work, thats why i need to find the value that i want to change

Here you go:
SELECT REPLACE(body, SUBSTRING(body, CHARINDEX(':', body) + 1, CHARINDEX('The page you can visite is', body) - (CHARINDEX(':', body) + 1)) ,' <br/> <br/> <br/> XXXX <br/><br/><br/><br/> ')
FROM #temp
Note that I used complete fragment "The page you can visit is" with the CHARINDEX to reduce the possibility of that same subs-string being present in the "HERE" section.

If your question can be answered using a regular expression replacing, which I think it can, use https://github.com/mysqludf/lib_mysqludf_preg it will provide you with functions such as PREG_REPLACE to replace content matching a regular expression with other data

Related

Replace text next to static text google script

I would like to replace the text in a google doc. At the moment I have place markers as follows
Invoice ##invoiceNumber##
I replace the invoice number with
body.replaceText('##invoiceNumber##',invoiceNumber);
Which is fine but I can only run the script once as obviously ##invoiceNumber## is no longer in the document. I was thinking I could replace the text after Invoice as this will stay the same, appendParagraph looks like it might to the trick but I can't figure it out. I think something like body.appendParagraph("Invoice") would select the area? Not sure how to append to this after that.
You could try something like this I think:
body.replaceText('InvoiceNumber \\w{1,9} ','InvoiceNumber ' + invoicenumber);
I don't know how big your invoice numbers are but that will except from 1 to 9 word characters preceeded by a space and followed by a space. That pattern might have to be modified depending upon your textual needs.
Word Characters [A-Za-z0-9_]
If your invoice numbers are unique enough perhaps you could just replace them.
Reference
Regular Expression Syntax
Note: the regex pattern is passed as a string rather than a regular expression

How to replace a word in html only if some conditions are met with regex

I try to replace every occurrence of a word in a text (which is a html file) and everything around until we meet a " or a ' or a ( for behind or a ) for forward with a regex using nodejs.
My problem is that when I have two words to replace let's say 3.png and 13.png, 13.png is being replaced too by matching 3.png and when I come to replace 13.png in my text it's not there because it was already replaced when matching previous 3.png.
My ideal solution would be :
if matched pattern contains a /
then it must exact match after / and replace everything around (slash included) until we meet one of these characters (excluded) " or a ' or a ( for behind or a )
else exact match between "" or '' or ()
You can find here a regex101 example
Currently I'm sorting my words to search like so:
imgjson.sort((a, b) => b.name.length - a.name.length);
in order to replace the longest words first which solves my problem because we replace 13.png first then 3.png but I would like to know if this can be done with js regex?
Thanks a lot for your reply and time!
As #PushpeshKumarRajwanshi told use \b.
If you want to be more accurate and informed about regex, you can use https://regex101.com/.
In right-bottom corner you can find all special characters and functions of regex you may be need to use.

Remove/strip specific Html tag and replace using NotePad++

Here is my text:
<h3>#6</h2>
Is he eating a lemon?
</div>
I have a few of them in my articles the #number is always different also the text is always different.
I want to make this out of it:
<h3>#6 Is he eating a lemon?</h3>
I tried it via regex in notepad++ but I am still very new to this:
My Search:
<h3>.*?</h2>\r\n.*?\r\n\r\n</div>
Also see here.
Now it is always selecting the the right part of the text.
How does my replace command need to look like now to get an output like above?
You should modify your original regex to capture the text you want in groups, like this:
<h3>(.*?)</h2>\r\n(.*?)\r\n\r\n</div>
( ) ( )
// ^ ^ These are your capture groups
You can then access these groups with the \1 and \2 tokens respectively.
So your replace pattern would look like:
<h3>\1 \2</h3>
Your search could be <h3>(.*)<\/h2>\r\n(.*)\r\n\r\n<\/div>
and the replace is <h3>$1 $2</h3>, where $1 and $2 represent the strings captured in the parentheses.

Regex find two characters in order, between others, ignoring punctuation

I'm trying to filter using regex in mySQL.
The field is a text field and I want to find all that match 'MD' or similar ('M.D.', 'M. D.', 'DDS, M.D.' etc.).
I do not want to accept those that contain M and D as a part of another acronym (e.g., 'DMD'). However 'DMD, M.D.' I would want to find.
Apologies if this is a simple task - I read through some regex tutorials and couldn't figure this out! Thanks.
Update:
With help from the suggestions I arrived at the following solution:
(\s|^)M\.?\s*D\.?
which works for all of my cases. The quotes in my questions were to indicate it was a string, they are not a part of the string.
You can use a regex like this:
\b(M\.?\s*D\.?|D\.?\s*D\.?\s*S\.?)
Working demo
If I have understood your requirement:
'([^'.]*[ ,]*M[. ]*D[. ]*)'
this looks for MD preceded by space comma or ' separated by 0 or more dots & spaces, followed by '
it matches all the contents between the '' marks
test: https://regex101.com/r/oV2kV8/2
In the end I found this solution works:
(\s|^)M\.?\s*D\.?(\s|$)
This allows for the 'MD' to be at the start or after another credential and to have spaces or periods or nothing between the letters.

Get Image with Xpath using class of Div

How do I write the xpath to get the main news image in this article?
The below one failed for me.
//div[contains(#class,'sectionColumns')]//div[contains(#class,'column2']//*img"]
I want it to return all images in case of slideshow. I want it to be flexible as some classes
change when news changes.
Without looking at "this article", there is an obvious syntax error in your XPath expression:
//div[contains(#class,'sectionColumns')]//div[contains(#class,'column2']//*img"]
The substring of the above:*img", contains two errors -- * followed by a name, and an unbalanced quote.
Probably you want:
//div[contains(#class,'sectionColumns')]//div[contains(#class,'column2']//img]