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 đ