CLASSIC ASP: Replace all characters from email string - less # and "." [duplicate] - email

This question already has answers here:
VBScript RegEx : Replace Content
(2 answers)
Closed 2 years ago.
I'm trying to replace all characters from an Email String - less # and "." with Classic ASP
Something like this:
JOHN.DOE#EMAIL.COM
****.***#*****.***
CARL_SAGAN#EMAIL.COM
**********#*****.***
I Try with "REPLACE(EMAIL, J, *)" - but with this I need to replace each characters (and this includes special characters too, like "_" "-" and others..)
There is any other alternative?
tks!
==
UPDATE:
I'm got a better solution with REGEX:
Dim regEx
Set regEx = New RegExp
regEx.Pattern = "[A-Za-z]"
regEx.Global = True
tipEmail = regEx.Replace(Email, "*")
But I'm not so expert. This RegEX Pattern excludes AZ/az and I need to exclude ALL characters - less # and "."
tks.

I got it!
the solution -
Dim regEx
Set regEx = New RegExp
regEx.Pattern = "[^#.]"
regEx.Global = True
tipEmail = regEx.Replace(Email, "*")

Related

Extracting range of unpadded string

I'd like to extract the Range<String.Index> of a sentence within its whitespace padding. For example,
let padded = " El águila (🦅). "
let sentenceRangeInPadded = ???
assert(padded[sentenceRangeInPadded] == "El águila (🦅).") // The test!
Here's some regex that I started with, but looks like variable length lookbehinds aren't supported.
let sentenceRangeInPadded = padded.range(of: #"(?<=^\s*).*?(?=\s*$)"#, options: .regularExpression)!
I'm not looking to extract the sentence (could just use trimmingCharacters(in:) for that), just the Range.
Thanks for reading!
You may use
#"(?s)\S(?:.*\S)?"#
See the regex demo.
Details
(?s) - a DOTALL modifier making . match any char, including line break chars
\S - the first non-whitespace char
(?:.*\S)? - an optional non-capturing group matching
.* - any 0+ chars as many as possible
\S - up to the last non-whitespace char.

Determine if a string only contains invisible characters in Swift

I was parsing a messy XML. I found many of the nodes contain invisible characters only, for instance:
"\n "
" "
"\t "
"\n "
"\n\n"
I saw some posts and answers about alphabet and numbers, but the XML being parsed in my project includes UTF8 characters. I am not sure how I can list all visible UTF8 characters in the filter.
How can I determine if a string is made up of completely invisible characters like above, so I can filter them out? Thanks!
Use CharacterSet for that.
let nonWhitespace = CharacterSet.whitespacesAndNewlines.inverted
let containsNonWhitespace = (string.rangeOfCharacter(from: nonWhitespace) != nil)
Trim the string of whitespaces and newlines and see what's left.
if someString.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
// someString only contains whitespaces and newlines
}

How to get hashtag from string that contains # at the beginning and end without space at the end?

This is my string
"I made this wonderful pic last #chRistmas... #instagram #nofilter #snow #fun"
and I would like to get hashtag that contains # at the beginning and end without space. My expected result is:
$fun
This is what I have so far for regex search:
#[a-z0-9]+
but it give me all the hashtags not the one that I want. Thank you for your help!
Using #[a-zA-Z0-9]*$ instead of your current regex
It seems you need to match a hashtag at the end of the string, or the last hashtag in the string. So, there are several ways solve the issue.
Matching the last hashtag in the string
let str = "I made this wonderful pic last #chRistmas... #instagram #nofilter #snow #fun"
let regex = "#[[:alnum:]]++(?!.*#[[:alnum:]])"
if let range = str.range(of: regex, options: .regularExpression) {
let text: String = String(str[range])
print(text)
}
Details
# - a hash symbol
[[:alnum:]]++ - 1 or more alphanumeric chars
(?!.*#[[:alnum:]]) - no # + 1+ alphanumeric chars after any 0+ chars other than line break chars immediately to the right of the current location.
Matching a hashtag at the end of the string
Same code but with the following regexps:
let regex = "#[[:alnum:]]+$"
or
let regex = "#[[:alnum:]]+\\z"
Note that \z matches the very end of string, if there is a newline char between the hashtag and the end of string, there won't be any match (in case of $, there will be a match).
Note on the regex
If a hashtag should only start with a letter, it is a better idea to use
#[[:alpha:]][[:alnum:]]*
where [[:alpha:]] matches any letter and [[:alnum:]]* matches 0+ letters or/and digits.
Note that in ICU regex patterns, you may write [[:alnum:]] as [:alnum:].
You can use:
(^#[a-z0-9]+|#[a-z0-9]+$)
Test it online

Is there a function to escape all regex-relevant characters?

The regex I'm using in my application is a combination of user-input and code. Because I don't want to restrict the user I would like to escape all regex-relevant characters like "+", brackets , slashes etc. from the entry.
Is there a function for that or at least an easy way to get all those characters in an array so that I can do something like this:
for regexChar in regexCharacterArray{
myCombinedRegex = myCombinedRegex.replaceOccurences(of: regexChar, with: "\\" + regexChar)
}
Yes, there is NSRegularExpression.escapedPattern(for:):
Returns a string by adding backslash escapes as necessary to protect any characters that would match as pattern metacharacters.
Example:
let escaped = NSRegularExpression.escapedPattern(for: "[*]+")
print(escaped) // \[\*]\+

How to eliminate a space character from a String? [duplicate]

This question already has answers here:
Does swift have a trim method on String?
(16 answers)
Closed 5 years ago.
In my app I have a text field, and when the user presses a button the text he have entered is added to a label. The problem is that if the user add an space at the end, the txt of the label looks bad.
Text field: "Diego "
Label: Great Diego !
Perform this on your String:
let trimmedString = yourstring.trimmingCharacters(in: .whitespacesAndNewlines)
It returns a new string made by removing from both ends of the String characters contained in a given character set, which in this case are whitesspaces and newlines. If you don't want to trim newlines, replace .whitespaceAndNewLines with .whitespace.
You can use String's trimming method passing in the characters to be trimmed. One way to do this is:
let string = " something "
string.trimmingCharacters(in: .whitespacesAndNewlines)
Which will remove whitespace and newlines from the beginning and the end of the string.