Smarty delete last character from string if it's '/' - smarty

I'm trying to the last character from a smarty variable.
I knwo that I can delete the last character using substr or also replace a character with replace.
But I want to delete the last character only if it's ending with /
For example my string is /homepage/, I would like to become /homepage
But If I have /homepage it should be /homepage
EDIT :
Should also work for the string /homepage/? -> /homepage?

You can use the Smarty regex_replace and match to \ if it's the last character with \/$ and replace it with empty string.
In the same way you can use the regex_replace to replace /? ending with ?
{$variable|regex_replace:"/\/$/":""|regex_replace:"/\/\?$/":"?"}

Use the Smarty variable modifier regex_replace:
{$url|regex_replace:"#/$#":""}

Related

Remove string before and after characters in bash

I have a string like : 2021_03_19/ 19-Mar-2021 11:55 -
stored in a variable a
I tried to extract from it the sequence: 2021_03_19, the second one after /"> sequence with the following script:
a=${a##'/">'}
a=${a%%'/</a'}
But the final result is the same string as the input.
The pattern in the parameter expansion needs to match the entire string you want to remove. You are trying to trim the literal prefix /"> but of course the string does not begin with this string, so the parameter expansion does nothing.
Try
a=${a##*'/">'}
a=${a%%'/</a'*}
The single quotes are kind of unusual; I would perhaps instead backslash-escape each metacharacter which should be matched literally.
a=${a##*/\"\>}
a=${a%%/\</a*}
You have to match the before and after pattern too.
a=${a##*'/">'}
a=${a%%'/</a'*}
You could use:
a='2021_03_19/ 19-Mar-2021 11:55 -'
b=${a#*>}
c=${b%%/<*}
Based on Extract substring in Bash
In your example you want to select based on 3 characters but have ##, not ###. I did try that but doesn't seem to work either. So, therefore an alternative solution.

edit attribute value in bash shell unix script

Guys i have an xml where they have requestIDattrbute and i want to change its value - it can be in lof of forms like this:
<ns0:requestID>12345</ns0:requestID>
<requestID>12345</requestID>
<requestID>12345667</requestID><anyOtherAttribute>131241</anyOtherAttribute>
any suggestion how to make it through "sed" ? - Thanks
any suggestion how to make it through "sed" ?
This simple substitution command handles the shown cases:
sed 's,\<requestID>[^<]*,requestID>CHANGE,'
can you tell me what does it means ?
s,regexp,replacement, - Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement.
\< - match the empty string at the beginning of a word
[^<] - matches any character but <
* - The preceding item will be matched zero or more times.
CHANGE - value to which you want to change

Replace a string with another that contains newlines

Let's say I have:
template=$(<template.txt)
replacement=$(<replacement.txt)
How do I then store in a third variable the value of template, with "REPLACE_ME" replaced with the content of replacement, no matter what template and replacement contain?
Try this with GNU bash 4:
foo="${template//REPLACE_ME/$replacement}"

Sed or String Replace command in Unix to change last First Character after Sequence to UpperCase

I basically have these xml files where I need to change the first alphabet after
Eg.
Result:
I tried: sed 's/<structure name=\"/\U\/g'
However, this changes the entire word to uppercase. Can someone help me out?
\U is for converting all characters. You will need to use \u to convert the first occurrence.
Also, you will need to group them to ensure correct letter is converted:
sed 's/\(<structure name=\"\)\(.\)/\1\u\2/' xml-file
sed 's/<structure name=\"\(.\)/<structure name=\"\U\1/'
sed will only convert strings being substituted to uppercase. We can use a capturing group to only convert the first character after the sequence to uppercase.
Otherwise, you can also use \E, which is similar to \U, except it stops converting characters instead of starting it.

Updating from ereg to preg_match

I read similar titles but I couldn't make it run..
Now, I have a code like this (originally ereg):
if (preg_match("[^0-9]",$qrcode_data_string)){
if (preg_match("[^0-9A-Z \$\*\%\+\-\.\/\:]",$qrcode_data_string)) {
I also tried using / at the beginning and end of rule but didn't work.
Any replies welcome.
With the preg_* functions you need delimiters around the pattern:
if (preg_match("#[^0-9]#", $qrcode_data_string)) {
# ^ ^
From the documentation:
When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).

Resources