-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
string replace should have options for both literal string replacement and regex replacement #346
Comments
@samer1977 Can you give an example ? |
OK, I was trying to solve the problem here: #342
To do that I have to get each capture , store in an array , then do the next capture recursively by purging the json through replace with empty string and so until no capture left. I understand that there is limitation where each capture has to be different and you can only have one key-value pair capture which would have worked for this scenario. The above function would have worked on more simplistic scenario like this: { "body" : "<div class="intercom-container"><img src="image1">. <div class="intercom-container"><img src="image2">" However once you introduce more complex string like urls then it wont because regex. |
Sorry to be a nag, can you give us a challenging example ?
|
Yes. Can you make the recursive function above work on the original input without having to escape every regex char ? { "body" : "<div class="intercom-container"><img src="https://downloads.intercomcdn.com/i/o/243069600/b5cf534d3975fafc7eafa9e7/IMG_2568.PNG?expires=1671464976&signature=6dc0e026cb490829b7e333f8254dd50358356f9bf3369339ddb3aaf11d14ca34\">. <div class="intercom-container"><img src="https://downloads.intercomcdn.com/i/o/24306960011/b5cf534d3975fafc7eafa9e7/IMG_2568.PNG?expires=1671464976&signature=6dc0e026cb490829b7e333f8254dd50358356f9bf3369339ddb3aaf11d14ca34\">" } |
How about this. input { "body": "<div class='intercom-container'><img src=\"image1\"></img></div><div class=\"intercom-container\"><img src=\"image2\"></img></div><div class=\"intercom-container\"><img src=\"image3\" /><div class=\"intercom-container\"><img src=\"image4\"/><img src='^&*image5'/></div>"
} You have different image tags
Here's the transformation:
No need to use recursion. The trick is to split up the input with the "seperator" Yes, there can be any funny characters in the src-attribute, even regexp "reserved" characters. The result is: [ {
"url" : "image1"
}, {
"url" : "image2"
}, {
"url" : "image3"
}, {
"url" : "image4"
}, {
"url" : "^&*image5"
}
] |
How did I come to this solution ? I first only used this
This gave me [
"<div class=\"intercom-container\">",
"src=\"image1\"></img></div><div class=\"intercom-container\">",
"src=\"image2\"></img></div><div class=\"intercom-container\">",
"src=\"image3\" /><div class=\"intercom-container\">",
"src=\"image4\"/>",
"src=\"^&*image5\"/></div>"
] As you can see, the first element in the array, does not have a "src=" at the beginning.
Now that all elements start with "src=", the regexp just becomes - basically anything betwen the quotes:
And now you wrap array processing around it resulting in "[ for ..... capture (...) ]". |
First of all, your input is not properly formatted; you should get plenty of errors in the sandbox alone. Here's how it should be: { "body": "<div class=\"intercom-container\"><img src=\"image1\">. <div class=\"intercom-container\"><img src=\"image2\">"
} Second, you are not properly regexing. You want to exclude anything that is blue, only capture the orange string. So, this below should work.
A few words of advise.
Instead of What is in: anything orange above, that means, a sequence of 1 or more characters EXCEPT for a double quote. The "in" part is expressed as A good resource is https://www.regular-expressions.info/charclass.html Good luck. |
OK! Thanks for your detailed answer. I appreciate it , at least its detailed. I will take everything you said into consideration and try to be careful when posting data\code. I did not pay much attention to what I was pasting because I made it clear early on that this all based on this: #342 and that should have been your source. No excuse though I will try and do better next time. I'm using all the above and I understand regex very well but sorry Im still human. |
We are all here to learn from each other. |
This PR addresses issues schibsted#346 and schibsted#347.
This PR addresses issues schibsted#346 and schibsted#347. Added documentation.
This PR addresses issues schibsted#346 and schibsted#347. Added documentation.
This PR addresses issues schibsted#346 and schibsted#347. Added support for Regexp pattern "Predefined character classes".
I created a PR, see #350. |
no doubt having the regex replace is very powerful but sometimes you want to do simple a literal string replace.Where I encountered a problem is when I wanted to replace literal string that contains regex chars. I could not find an easy way to do that but having to replace all regex reserved char first to escape them and that can get cumbersome and inefficient.
The text was updated successfully, but these errors were encountered: