How do I use REPLACE in Asterisk to escape input - asterisk

I want to escape \ to \\ and " to \" in Asterisk. I've tried to use REPLACE but I can't get it to work. My current approach is as follows
exten => sms,1,Set(UNSAFESMSTXT=${REPLACE(SMSTXT,\\,\\\\)})
exten => sms,2,Set(SAFESMSTXT=${REPLACE(UNSAFESMSTXT,",\\")})
; Echo escaped input to terminal safely
exten => sms,n,System(echo "${SAFESMSTXT}")
When I send echo hi"echo hi, I get the error
ERROR[8239]: func_strings.c:804 replace: The characters to search for and the variable name must not be empty.
I use chan_mobile if that helps.

What is nto cleat in function description?
Asterisk func REPLACE
Synopsis:
Replace a set of characters in a given string with another character.
Description:
Iterates through a string replacing all the <find-chars> with <replace-ch
ar>. <replace-char> may be either empty or contain one character. If empty,
all <find-chars> will be deleted from the output.
NOTE: The replacement only occurs in the output. The original variable is
not altered.
Syntax:
REPLACE(varname,find-chars[,replace-char])
You have use \" instead of "

Related

Split a string based on Pipe delimiter and except inside brackets ()

I want to split a string by '|' delimiter and exclude those inside the brackets (). I used tokenize function to split by delimiter and have some issue with exclusion part(RegEx format). Please help.
Input: Test|Test1|Test2|(Test3|Test4)|Test5|(Test6)(Test7)|Test8
Output: Test,Test1,Test2,(Test3|Test4),Test5,(Test6)(Test7),Test8
Thanks in advance.
Perhaps the analyze-string function suffices to break up the string, I don't think the single example makes the rules clear but it would be like
analyze-string('Test|Test1|Test2|(Test3|Test4)|Test5|(Test6)(Test7)|Test8','(\([^)]*\))+')/*
/(if (. instance of element(fn:match)) then data() else tokenize(., '\|'))[normalize-space()]
either => string-join(',') that result or use
declare option output:method 'text';
declare option output:item-separator ',';
analyze-string('Test|Test1|Test2|(Test3|Test4)|Test5|(Test6)(Test7)|Test8','(\([^)]*\))+')/*
/(if (. instance of element(fn:match)) then data() else tokenize(., '\|'))[normalize-space()]

ADA - Searching a directory with a pattern - not returning as it should

This section of my program is supposed to list all files within the directory containing ".txt" in the name but it's not returning anything when run. If I delete ".txt" and leave it as an empty string "" then it works perfectly and returns all file names including the .txt files so I can't figure out what I'm doing wrong here.
procedure Search_Directory is
use Ada.Directories;
procedure Write_Search_Item(Search_Item : in Directory_Entry_Type) is
begin
Put(Item => Simple_Name(Directory_Entry => Search_Item));
New_Line;
end Write_Search_Item;
Filter : Constant Filter_Type := (Ordinary_File => True,
Special_File => False,
Directory => True);
begin
Search(Directory => Current_Directory,
Pattern => (".txt"),
Filter => Filter,
Process => Write_Search_Item'Access);
end Search_Directory;
The Search function, defined in the package Ada.Directories, takes a pattern argument which is either a null string or a form that is implementation-defined RM A.16 (111/ 2). In GNAT, this pattern is supposed to be a regular expression (see also here) described in System.Regexp (see also here, second grammar, a "globbing pattern").

KSH script checks for alphanumeric in case statement

Below is a simplified model of what I'm trying to achieve:
#!bin/ksh
string=AUS00
case $string in
[[:alnum:]] ) echo "alphanumeric" ;;
*) echo "nope" ;;
esac
I'm unable to validate alphanumeric code.
Constraints:
The validation need to happen inside the case statement
alnum function is not supported
Positive check only. Can't check for the absence of alphanumeric.
Thank you very much
The pattern [[:alnum:]] will match a single alphanumeric character. Your string is longer than one character, so it won't match.
If you want to check that your string contains an alnum character, you want *[[:alnum:]]*
If you want to check that your string only contains alnum characters, I'd flip the check to see if the string contains a non-alnum character:
for string in alnumOnly 'not all alnum'; do
case "$string" in
*[^[:alnum:]]*) echo "$string -> nope" ;;
*) echo "$string -> alphanumeric" ;;
esac
done
alnumOnly -> alphanumeric
not all alnum -> nope
I realized that ksh (even ksh88) implements what bash describes as "extended patterns":
A pattern-list is a list of one or more patterns separated
from each other with a |. Composite patterns can be formed
with one or more of the following:
?(pattern-list)
Optionally matches any one of the given patterns.
*(pattern-list)
Matches zero or more occurrences of the given
patterns.
+(pattern-list)
Matches one or more occurrences of the given patterns.
#(pattern-list)
Matches exactly one of the given patterns.
!(pattern-list)
Matches anything, except one of the given patterns.
So we can do:
case "$string" in
+([[:alnum:]]) ) echo "$string -> alphanumeric" ;;
* ) echo "string -> nope" ;;
esac

How to replace special characters using regex

Using Asp.net for regex.
I've written an extension method that I want to use to replace whole words - a word might also be a single special character like '&'.
In this case I want to replace '&' with 'and', and I'll need to use the same technique to reverse it back from 'and' to '&', so it must work for whole words only and not extended words like 'hand'.
I've tried a few variations for the regex pattern - started with '\bWORD\b' which didn't work at all for the ampersand, and now have '\sWORD\s' which almost works except that it also removes the spaces around the word, meaning that a phrase like "health & beauty" ends up as "healthandbeauty".
Any help appreciated.
Here's the extension method:
public static string ReplaceWord(this string #this,
string wordToFind,
string replacement,
RegexOptions regexOptions = RegexOptions.None)
{
Guard.String.NotEmpty(() => #this);
Guard.String.NotEmpty(() => wordToFind);
Guard.String.NotEmpty(() => replacement);
var pattern = string.Format(#"\s{0}\s", wordToFind);
return Regex.Replace(#this, pattern, replacement, regexOptions);
}
In order to match a dynamic string that should be enclosed with spaces (or be located at the start or end of string), you can use negative lookaheads:
var pattern = string.Format(#"(?<!\S){0}(?!\S)", wordToFind);
^^^^^^^ ^^^^^^
or even safer:
var pattern = string.Format(#"(?<!\S){0}(?!\S)", Regex.Escape(wordToFind));
^^^^^^^^^^^^^
The (?<!\S) lookbehind will fail the match if the word is not preceded with a non-whitespace character and (?!\S) lookahead will fail the match if the word is not followed with a non-whitespace character.

How do I create a function to check if a string only consists of A-Z , 0-9

Is there any way to check in Xquery (A Xquery function) if an input string has only characters A-Z or numbers 0-9 and no other characters.
for example if the string is ABZ10 the function should return true and if the input string is 5& 123x it returns a false.
I am able to do it in java by simply using following.
inputstring.matches("^[0-9A-Z]+$"))
Use:
matches($vYourString, '^[A-Z0-9]+$')

Resources