removing special characters in asp - asp-classic

I want to identify special characters and remove that special characters from my string or a word
for example
O'neil - i want to remove (') from this word.
Muñoz, A. Patrick - i want to remove above character of n (ñ)
similarly i want to remove all special characters from my strings.
I want to do this in asp
How can i do this

You could use a regular expression and then run the following. You'll need to change the regular expression accordingly.
Const PATTERN = "\W"
Dim objRegEx
Dim strReplacedString : strReplacedString = ""
Set objRegEx= New RegExp
objRegEx.Pattern = PATTERN
objRegEx.IgnoreCase = True
objRegEx.Global = True
strReplacedString = objRegEx.Replace(strToProcess,"")
Set objRegEx = Nothing

Related

Remove all whitespace from string AX 2012

PurchPackingSlipJournalCreate class -> initHeader method have a line;
vendPackingSlipJour.PackingSlipId = purchParmTable.Num;
but i want when i copy and paste ' FDG 2020 ' (all blanks are tab character) in Num area and click okey, write this value as 'FDG2020' in the PackagingSlipId field of the vendPackingSlipJour table.
I tried -> vendPackingSlipJour.PackingSlipId = strRem(purchParmTable.Num, " ");
but doesn't work for tab character.
How can i remove all whitespace characters from string?
Version 1
Try the strAlpha() function.
From the documentation:
Copies only the alphanumeric characters from a string.
Version 2
Because version 1 also deletes allowed hyphens (-), you could use strKeep().
From the documentation:
Builds a string by using only the characters from the first input string that the second input string specifies should be kept.
This will require you to specify all desired characters, a rather long list...
Version 3
Use regular expressions to replace any unwanted characters (defined as "not a wanted character"). This is similar to version 2, but the list of allowed characters can be expressed a lot shorter.
The example below allows alphanumeric characters(a-z,A-Z,0-9), underscores (_) and hyphens (-). The final value for newText is ABC-12_3.
str badCharacters = #"[^a-zA-Z0-9_-]"; // so NOT an allowed character
str newText = System.Text.RegularExpressions.Regex::Replace(' ABC-12_3 ', badCharacters, '');
Version 4
If you know the only unwanted characters are tabs ('\t'), then you can go hunting for those specifically as well.
vendPackingSlipJour.PackingSlipId = strRem(purchParmTable.Num, '\t');

ASP.Net RegEx with ampersand and spaces

I am using the following regular expression to find words and phrases in a document. (Have to use regular expression and have to use \b.)
\b (zoo|a & b|dummy)\b
When I try to find matches in the following string
going to the zoo with a & b
a & b doesn't get matched. However, if I remove the leading and following space from the string and regex, making both a&b, it matches, but I do need to those spaces.
Use \s for spaces
string strRegex = #"\b\s(zoo|a\s&\sb|dummy)\b";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = #"going to the zoo with a & b";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
Console.WriteLine(myMatch);
}

Find word (not containing substrings) in comma separated string

I'm using a linq query where i do something liike this:
viewModel.REGISTRATIONGRPS = (From a In db.TABLEA
Select New SubViewModel With {
.SOMEVALUE1 = a.SOMEVALUE1,
...
...
.SOMEVALUE2 = If(commaseparatedstring.Contains(a.SOMEVALUE1), True, False)
}).ToList()
Now my Problem is that this does'n search for words but for substrings so for example:
commaseparatedstring = "EWM,KI,KP"
SOMEVALUE1 = "EW"
It returns true because it's contained in EWM?
What i would need is to find words (not containing substrings) in the comma separated string!
Option 1: Regular Expressions
Regex.IsMatch(commaseparatedstring, #"\b" + Regex.Escape(a.SOMEVALUE1) + #"\b")
The \b parts are called "word boundaries" and tell the regex engine that you are looking for a "full word". The Regex.Escape(...) ensures that the regex engine will not try to interpret "special characters" in the text you are trying to match. For example, if you are trying to match "one+two", the Regex.Escape method will return "one\+two".
Also, be sure to include the System.Text.RegularExpressions at the top of your code file.
See Regex.IsMatch Method (String, String) on MSDN for more information.
Option 2: Split the String
You could also try splitting the string which would be a bit simpler, though probably less efficient.
commaseparatedstring.Split(new Char[] { ',' }).Contains( a.SOMEVALUE1 )
what about:
- separating the commaseparatedstring by comma
- calling equals() on each substring instead of contains() on whole thing?
.SOMEVALUE2 = If(commaseparatedstring.Split(',').Contains(a.SOMEVALUE1), True, False)

How do I delete characters in a string up to a certain point in classic asp?

I have a string that at any point may or may not contain one or more / characters. I'd like to be able to create a new string based on this string. The new string would include every character after the very last / in the original string.
Sounds like you're wanting the file name from a URL. In any case, it's the same function. The key is using the InStrRev function to find the first / char, but starting from the right. Here's the function:
Function GetFilename(URL)
Dim I
I = InStrRev(URL, "/")
If I > 0 Then
GetFilename = Mid(URL, I + 1)
Else
GetFilename = URL
End If
End Function
Split it up into parts and get the last part:
a = split("my/string/thing", "/")
wscript.echo a(ubound(a))
note: Not safe when the string is empty.

Digits Regular Expression Validator

i need a regular expression validation for
numeric digits grouped as X-XXXXX-XXX-X
can any one help?
Regex reg = new Regex("\b[0-9]\-[0-9]{5}\-[0-9]{3}\-[0-9]\b");
Here is what I use for checking social security numbers that user's input:
Public Shared Function CheckSSNFormat(ByVal text As String) As Boolean
Dim digits As String = Regex.Replace(text, "[^0-9]", "")
Return digits.Length = 9
End Function
It doesn't check that they are input in a specific format, but that might be better depending on what you really need -- so just thought I'd give you another option, just incase.
The above just removes everything except digits, and returns true if there are 9 digits (a valid SS#). It does mean some goofy user could enter something like: hello123456789 and it would accept it as valid, but that is fine for me, and I'd rather do that than not accept 123456789 just because I was looking for 123-45-6789 only.
Later I use this to save to my database:
Public Shared Function FormatSSNForSaving(ByVal text As String) As String
If text = "" Then text = "000-00-0000"
Return Regex.Replace(text, "[^0-9]", "")
End Function
and this anytime I want to display the value (actually I use this one for phone numbers, turns out I never display the SS# so don't have a function for it):
Public Shared Function FormatPhoneForDisplay(ByVal text As String) As String
If text.Length <> 10 Then Return text
Return "(" & text.Substring(0, 3) & ") " & text.Substring(3, 3) & "-" & text.Substring(6, 4)
End Function
(^\d{1}-\d{5}-\d{3}-\d{1}$), this should do.
[0-9]-[0-9]{5}-[0-9]{3}-[0-9]
you could also use round brackets to extract the numbers if you want:
([0-9])-([0-9]{5})-([0-9]{3})-([0-9])
and get the values with $1 $2 etc. in the Regex.Replace() function
Regex pattern = new Regex("\b\d-\d{5}-\d{3}-\d\b");
\b - word boundary
\d - digit

Resources