0

I have a string like following:

string strAttachment = "[email protected],3470SQL.txt";  

i want this:

cosmeticsview.png,SQL.txt

i try this:

var result = Regex.Replace(strAttachment, @"\d+@+$", "");
Response.Write(result);

but not work.

i think causes this problem is $ symbol.

Edit

I want remove digits also

6
  • You didn't ask the similar question around 30 minutes ago? Yeap you did stackoverflow.com/questions/19656833/… Commented Oct 29, 2013 at 12:07
  • yes but $ symbol not worked Commented Oct 29, 2013 at 12:09
  • @SonerGönül that was about JPY, now its USD :) Commented Oct 29, 2013 at 12:09
  • Here's what I'm not understanding. You're looking for 1 or more digits and then 1 or more @, and then the end of the line. This Regex is so far from what you want, I just don't get it. I really don't. Commented Oct 29, 2013 at 12:11
  • @lazyberezovsky , do you know? Commented Oct 29, 2013 at 12:12

3 Answers 3

1

You have two problems here - first is escaping of special regex symbols (end of line in your case). And second one is wrong pattern - your current solution tries to match @ right after digits:

   one or more @ character
       |
       | end of line
       | |
    \d+@+$
     |
one or more digit

That will be matched by hello1234@ which, of course, not your case. You want to remove either digits with dollar sign, or @ character. Here is correct pattern:

  one optional $ character
        |
        | OR
        | |
    \d+\$?|@
     |     |
     |   @ character
     |
one or more digit

Here is code:

string strAttachment = "[email protected],3470SQL.txt";  
var result = Regex.Replace(strAttachment, @"\d+\$?|@", "");

Alternatively, if you just want to remove any digits, dollars and @ from your string:

var result = Regex.Replace(strAttachment, @"[\d\$@]+", "");
Sign up to request clarification or add additional context in comments.

1 Comment

@SamieyMehdi 3470SQL.txt do not have dollar sign. Any digit should be removed?
1

try escaping it like \$

it's reserved in Regex to indicate the end of a line

Also, the @+ isn't needed - that's not how regex works. To get your desired result you want:

\d+\$

then use a Replace for the @:

var result = Regex.Replace(strAttachment, @"\d+$", "").Replace("@","");

Comments

0

I think you're complicating this by trying to make a single pass. First get the part of the string you want (e.g. [email protected],3470SQL.txt):

var match = Regex.Match(strAttachment, @"\$(.*)");
if (!match.Success) { return; }

then strip the characters you don't want. Define a list like this somewhere:

List<char> invalidChars = new List<char> { '@' };

In my example I only added the @.

Now just remove those characters:

var val = string.Join("",
    match.Groups[1].Value
        .Where(c => !invalidChars.Contains(c))
);

Here is a Regex 101 to prove the Regex I provided.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.