Ash_and_Perl
Ash_and_Perl

Reputation: 358

How to remove a dot from a dynamically created URL

In my application, we have a template; where the user can edit and then save it to the DB. The Template is then used by our application to send email invites.

Here is the draft template:

Dear {USER FIRST NAME} {USER LAST NAME}: 

{COLLEGE NAME} is providing your access to `example` services, including a dedicated online account. Registration was started for you. 

Without delay, please finish the last few items of your registration. 

Please promptly
finish your registration here: {REGISTRATION URL} 

Also, please {LOG IN} for the first time, to be sure you encounter no issues. If you have questions, [email protected] is available to help. Once you finish registration, access to your secure account and related example member services will stay with you as you proceed through learning experiences, within and beyond coursework. Thank you

Just before sending the invites, the application is replacing the KEYS with real user data. Like {USER FIRST NAME} is replaced with RAHUL and so on.

The problem which I am facing is ... the user edits the template and adds a dot(.) at the end of the {REGISTRATION URL}.

LIKE THIS:

Please promptly finish your registration here: {REGISTRATION URL}.

So, this eventually turns out to be https://www.example.com/reg?invite_id=1f2sdfg23. in the email body. The invite code is corrupted now. This is causing the problem to the user as they will be shown an error message. Invite not found page.

How can I contain this situation. We have informed the user not to do that but I want to preempt the situation even if the user unknowingly does it. I do not want any non-alpha numeric character after the 1f2sdfg23. It should always be followed by a line-break.

I tried the below regular expresssion:

    $email_body =~ s/\{REGISTRATION URL\}([^a-zA-Z])/$registration_url/ieg;

This is working for one instance of dot.

What if the user enters multiple dots Please promptly finish your registration here: {REGISTRATION URL}... {REGISTRATION URL}# etc..

$email_body =~ s/\{REGISTRATION URL\}([^a-zA-Z]+)/$registration_url/ieg;

If I extend the filter by allowing multiple characters, then it is removing the unwanted characters. But the problem, re-occurs when the user has promptly given {REGISTRATION URL}. The above line of code will remove the line spacing. I do not want that to happen.

Let me know what best approach I can apply here. Thanks in Advance.

Upvotes: 2

Views: 343

Answers (3)

tripleee
tripleee

Reputation: 189487

A common convention is to embed URLs in <brokets> in plain text to show exactly where the URL begins and ends.

No misunderstanding: <http://stackoverflow.com/>!

Upvotes: 1

Jorge_Freitas
Jorge_Freitas

Reputation: 150

Perhaps the best approach would be to check your file before starting the process of replacing those variables. If you expect that line to be finish your registration here: {REGISTRATION URL}, then you can check if that line is what you were expecting like this: $checkEmailTag =~ /\s\{REGISTRATION URL\}$/. In this case, a preceding space, then your tag and then the end of the line.

If it's not, then you can throw an error stating your file is corrupted and abort the process, or add a space so that user characters won´t hurt your URL allowing you to continue, or any other course of action you see fit.

You don't need to do this for every variable in your file, just for critical variables like this one.

Upvotes: 0

mrzasa
mrzasa

Reputation: 23327

As wrote in the comment - I can see two solutions

1) add a space after the variable

($email_body =~ s/\{REGISTRATION URL\}/$registration_url.' '/ieg;) 

2) replace with space everything that is not a whitespace

($email_body =~ s/\{REGISTRATION URL\}\S+/$registration_url/ieg)

Upvotes: 1

Related Questions