cancel
Showing results for 
Search instead for 
Did you mean: 

Need UDF to allow few characters

Former Member
0 Kudos

Hi Experts,

I need to create UDF to allow only value with A-Z, a-z, 0-9, !@#$% & (),.<>;:’”[]{}-+=\ for all other characters i need to replace with empty string.

Please help on creation of UDF for this requirement.

Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

RaghuVamseedhar
Active Contributor
0 Kudos

Mohan,

UDF:

return var1.replaceAll("[^a-zA-Z0-9!@#$%&(),.<>;:'\"\\[\\]\\{\\}\\-+=\\\\]", "");

Former Member
0 Kudos

Thanks Raghu.

This is also removing all spaces.

Expected: Start special instructions added

Result i am getting now: Startspecialinstructionsadded

RaghuVamseedhar
Active Contributor
0 Kudos

Mohan,

Please use

return var1.replaceAll("[^a-zA-Z0-9\\s!@#$%&(),.<>;:'\"\\[\\]\\{\\}\\-+=\\\\]", "");

Former Member
0 Kudos

Thank you Raghu. It worked.

Answers (1)

Answers (1)

former_member181985
Active Contributor
0 Kudos

Hi,

Try the below java code in UDF


//Note:- str variable is UDF input

char[] bufferStr = str.toCharArray();

String otherChars = "!@#$%&(),.<>;:\'\"[]{}-+=\\";

char[] bufferOtherChars = otherChars.toCharArray();

StringBuffer sb = new StringBuffer();

for(int i = 0; i < bufferStr.length; i++)

{

    if((bufferStr[i] >= 'A' && bufferStr[i] <= 'Z') || (bufferStr[i] >= 'a' && bufferStr[i] <= 'z') || (bufferStr[i] >= '0' && bufferStr[i] <= '9'))

    {

        sb.append(bufferStr[i]);

    }else

    {

        for (int j = 0; j < bufferOtherChars.length ; j++ )

        {

            if(bufferStr[i] == bufferOtherChars[j])

            {

                sb.append(bufferStr[i]);

                break;

            }

        }

    }

}

return sb.toString();

Br,

Praveen

Former Member
0 Kudos

Thank you Praveen.

This UDF is removing all spaces as well, which is not needed.

Where do i change the code for getting spaces as it is?