Help with regex and find/replace
-
Hi everyone 😊
I’m trying unsuccessfully for a regex to try and replace this code.Find
[data=‘user1’ pid=‘123456’ dateline=‘1546943844’]message[/data]Replace with
[data author=user1 link=msg=123456 date=1546943844]message[/data]user1, 123456, 1546943844 and message are variable data.
Is it possible to do it with regex or am I going crazy for nothing?
Many thanks.
-
Welcom, @Massimo,
I am assuming those are really normal-quotes, not smart-quotes. Please see this awesome thread which explains how to use markdown to format your posts, so that the forum doesn’t change you important characters into smart characters.
what are the rules for each of the “variable data” contents? something like “data can be any alphanumeric sequence”, “pid and dateline must be numeric” – or “they can contain anything, as long as it’s not a quotemark”
Did you want quotes around the values in the replace, or not? There was a red flag when I saw
link=msg=123456
…Assuming normal quotes in source, no quotes in replace, and any non-quote allowed in the variable-data, I would do:
[data='user1' pid='123456' dateline='1546943844']message[/data]
-
find =
data='([^']*)' pid='([^']*)' dateline='([^']*)'
-
replace =
data=$1 link=msg=$2 date=$3
-
mode = regular expression
[data=user1 link=msg=123456 date=1546943844]message[/data]
If the order of the data/pid/dateline might change from row to row, or if you want to include lines that have
[data...]message[/data]
but exclude lines that have[data...]different text[/data]
then you will have to give us a longer data set, with examples of lines that will and won’t be modified. Please show the examples using Markdown, so we get the appropriate data, and don’t have to guess----
boilerplate that I include in search-and-replace threads:FYI: if you have further search-and-replace (regex) needs, study this FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting the data so that the forum doesn’t mangle it (so that it shows “exactly”, as I said earlier), see this help-with-markdown post, where @Scott-Sumner gives a great summary of how to use Markdown for this forum’s needs.
Please note that for all “regex” queries – or queries where you want help “matching” or “marking” or “bookmarking” a certain pattern, which amounts to the same thing – it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match. -
-
@PeterJones said:
Welcom, @Massimo,
I am assuming those are really normal-quotes, not smart-quotes. Please see this awesome thread which explains how to use markdown to format your posts, so that the forum doesn’t change you important characters into smart characters.
Hi Peter, thanks for your help!
Are smart-quotes that in dabase are saved as
'
This worked perfectly!
Find
data='([^']*)' pid='([^']*)' dateline='([^']*)'
Replace with
data=$1 link=msg=$2 date=$3
Thanks again 😉