Validate a string with Regex

Asked

Viewed 140 times

0

I need to validate the name of a file that has the following name NWD[0-9] PADRAO -. The literal part will always be the same, what may vary are the numbers.

I’d like to know how to do that with Regex.

  • 1

    Then some examples would be: "NWD0 PADRAO -", "NWD1 PADRAO -", "NWD2 PADRAO -"?

  • Yeah, that’s right.

  • Folks came across another problem. I will have to remove this part of the file name and replace it with blank space. Example filename.Replace("NWD1 PATTERN - ", "") filename.Replace("NWD2 PATTERN - ", ") filename.Replace("NWD3 PATTERN - ", "") If anyone can help me one more time, please! Thank you in advance for your attention!

  • So it means the name doesn’t just have NWD0 PADRAO - and yes other things back ? Otherwise removing that part gets no name at all which is not valid for a file name

  • So, it’s the name and a date. Example NWD PADRAO - 201802.xls

2 answers

3


Regex is almost the one you put in the question, but adding the "+" operator that indicates "one or more occurrences from the list". Example:

/NWD[0-9]+ STANDARD -/

See here the example in action

3

The Regex that will solve your problem is:

(NWD+[0-9]*)( PADRAO \-)

That means it will always seek to NWD followed by characters of 0 a 9 um número qualquer de vezes and finally seek to "espaço" PADRAO "espaco" -

This working regex can be checked here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.