Hi Everyone - I could do with some help with a reg...
# cfml-general
g
Hi Everyone - I could do with some help with a regular expression, PLEASE. After getting some help with doing a join for my SQL - I am now up to manipulating the original string and inserting the syntax I was helped with - which I was planning to do with RegEx. I need to do a search and replace on all column names that have "_response" somewhere / anywhere in them. I have a sample at regex101 I have been playing with it for about an hour now. I am aware that I will probably need to use a group for the words that start with "x." and an extra group (at least one) for the rest of the matches. (\bx\..?_response.?\b,|SOMETHINGHERE) Because the SQL is dynamically created I have no idea what the start of the word might be, or the end. Sometimes the column ends in "_response," - just like the first line in the fiddle. But other times it could be "AAA_response" or "BBB_response_CCC". In the fiddle they all end with "_reason" - but that is just lazy typing and not to be relied upon. Thanks - again!
a
Sorry, if I can't be arsed wading through that other thread, would you be able to reword the question / give a cliff's notes version of what what you wanna match & what you wanna replace it with? I cant work out what yer actually asking from... the post I'm replying to... because it sounds like a really drawn out way of saying "I wanna replace
_response
with something else" (and what 'someting else' is is actually unstated in yer Q as far as I can tell). But it can't be as simple as that cos that's just
s.replace("_response", "something else", "all")
and would not involve regexes at all. Plus I doubt you'd be asking a Q for which that's the answer 😉
g
Hi Adam, I want to "select" all instances of the entire word that contains "_response" anywhere within the word, in the whole string. Assuming that I have a 400 word SQL - where these three words are somewhere within that 400 words, AAA_response AAA_response_BBBB x.AAA_response I want a regex that will find them all. Either reMatch() or reFind() - i don't recall which - but one of them will return an array of matches it found. (Note: for THIS query it is AAA_response - for another it's "ZZZ_reponse" or "HHGFHG765765_response_GHJGJH1322GYHJ") The only "certainty" is that there is "_response" somewhere in the word. It shouldn't matter if there is a comma after it, before it, or not.
a
Gotcha. And it's def "word characters" making up the word? So letters, digits, underscore only? (see
\w
@ https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-lang[…]gular-expressions-in-functions/regular-expression-syntax.html)? And you just want an array of the matched words; you don't want to replace them with anything?
g
Awesome - thanks very much!
a
I think it was the
\w
option that you needed to know about yeah? It's handy to know about all those escape sequences. Even if you don't remember them all, it's good to review so you know what kind of thing might be doable.
BTW, years ago I discussed all aspects of regex usage in CFML: https://blog.adamcameron.me/2015/01/regular-expressions-in-cfml-link-summary.html (anything added since 2015 won't be mentioned, obvs)
👍🏼 1
g
I thought that what I wanted was the \b - but yeah \w for the win! I have read your posts about Regex, just a little earlier today... Maybe I should just head to your blog and do a search from now on? 😄
😜 1