I need help in my regex.
I need to remove the special character found in the start of text
for example I have a text like this
.just a $#text this should not be incl#uded
The output should be like this
just a text this should not be incl#uded
I've been testing my regex here but i can't make it work
([\!-\/\;-\@]+)[\w\d]+
How do I limit the regex to check only the text that starts in special characters?
Thank you
Use \\B[!-/;-@]+\\s*\\b
:
var result = Regex.Replace(s, @"\B[!-/;-@]+\s*\b", "");
See the regex demo
Details
\\B
- the position other than a word boundary (there must be start of string or a non-word char immediately to the left of the current position) [!-/;-@]+
- 1 or more ASCII punctuation \\s*
- 0+ whitespace chars \\b
- a word boundary, there must be a letter/digit/underscore immediately to the right of the current location. If you plan to remove all punctuation and symbols, use
var result = Regex.Replace(s, @"\B[\p{P}\p{S}]+\s*\b", "");
See another regex demo .
Note that \\p{P}
matches any punctuation symbols and \\p{S}
matches any symbols.
Use lookahead:
(^[.$#]+|(?<= )[.$#]+)
The ^[.$#]+
is used to match the special characters at the start of a line.
The (?<= )[.$#]+)
is used to matching the special characters at the start of a word which is in the sentence.
Add your special characters in the character group []
as you need.
Following are two possible options from your question details. Hope it will help you.
string input = ".just a $#text this should not be incl#uded";
//REMOVING ALL THE SPECIAL CHARACTERS FROM THE WHOLE STRING
string output1 = Regex.Replace(input, @"[^0-9a-zA-Z\ ]+", "");
// REMOVE LEADING SPECIAL CHARACTERS FROM EACH WORD IN THE STRING. WILL KEEP OTHER SPECIAL CHARACTERS
var split = input.Split();
string output2 = string.Join(" ", split.Select(s=> Regex.Replace(s, @"^[^0-9a-zA-Z]+", "")).ToArray());
Negative lookahead is fine here :
(?![\.\$#].*)[\S]+
https://regex101.com/r/i0aacp/11/
[\\S]
match any character
(?![\\.\\$#].*)
negative lookahead means those characters [\\S]+
should not start with any of \\.\\$#
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.