Regex code

I am looking to use regex to retrieve the “Name” from these types of file names: essentially just the word after the dash. What would the regex code for this be?

Weekly report - Name.xlsx
New week report - Name (002).xlsx
Report 1 - Name 32.xls

@UiUser

Try this.

  str = "Weekly report - Name"

  requiredStr = str.Split("-"c)(1)

I don’t think that would take care of the variations in the string input…is there a solution that uses regex or the match activity that would be more robust to multiple variations as an input @lakshman

Hi @UiUser

Try this:

-\s*(\w+)

3 Likes

@Shubham_Varshney thank you that works but there is a dash and space in front of the name is there a code that would eliminate these?

Ex output:
“- Name”

@UiUser
let’s assume the output of the regex is “STR”

“STR(1).trim” would help you out on that :slight_smile:

2 Likes

@UiUser

Try this

  1. Filename with extension (?m)(?<=-\s).

image

  1. Filename without extension (?m)(?<=-\s)[^.]

image

Thanks

1 Like

Hi,

The following expression might help you.

System.Text.RegularExpressions.Regex.Match(strData,"(?<=-\s*).+").Value

Regards,

2 Likes

Thank you @Yoichi

1 Like