Regex to get substring from string

The string is
“9969 JUHU AVE
INDIANA, MA 92335-6720”

I need to extract the last number i.e “92335-6720”.

Note: The string is variable and nothing is static.

Hi @Tanmay_V_Chetule

Try this

(?<=MA ).*(?=”)

image

(?<=\w+ ).*(?=”)

image

I hope it helps!!

MA Value will also change in other cases… any other soln?

@Tanmay_V_Chetule

see 2nd one

hey sorry, the string doesnt contain " "… the string is : 9969 JUHU AVE
INDIANA, MA 92335-6720

Hi @Tanmay_V_Chetule ,

We could also Check with the Split method :

Split(YourStrVar).LastOrDefault

Assuming that the last data is always separated by a Space.

If there is no data then it gives out Nothing as the value.

@Tanmay_V_Chetule

Try this

(?<=\w+ ).*(\d)

image

@Tanmay_V_Chetule

Try this

Vare1.Split(" "c).Last

@Tanmay_V_Chetule

Use the below regex expression

System.Text.RegularExpressions.Regex.Match(YourInput String Variable,"(\d+\-\d+)").Value

Hope it works!!

@Tanmay_V_Chetule

You can try this

[\d-]+$

System.Text.RegularExpressions.Regex.Match(str,"[\d-]+$").Value

Cheers

Hi @Tanmay_V_Chetule

- Assign -> strvar = "9969 JUHU AVE 
                             INDIANA, MA 92335-6720"

- Assign -> Output = strvar.Split(",")(1).Trim.Split(" ")(1).Trim.ToString

Output is 92335-6720

Use the split operation and split with comma and space

Hope it helps!!

1 Like

Hey @Tanmay_V_Chetule

You have multiple options posted in this thread, I like @Anil_G’s offer.

I have another option for you to consider:
(?<=[A-Z]{2}\s+)[\d-]+

Preview the pattern here

Cheers

Steve

2 Likes

Also you can use the free website regexstorm to check your regex if you needed. The other members given to us a great solutions.

2 Likes

Hi Tanmay,

Adding on to the other answers above, I would like to mention that if the last “number” is not a numeric string (with a hyphen) but an alphanumeric string where any special character can be present, then your solution will pretty much break and you may end up receiving partial or no result at all.

Example 1: Input String → “9969 JUHU AVE
INDIANA, MA 92335A-6720”
Regex: “[\d-]+$”
Output: -6720

Example 2: “9969 JUHU AVE
INDIANA, MA 92335-ABC”

Regex: “(?<=[A-Z]{2}\s)[\d-]+”
Output: “92335-”

Therefore, do check what the input pattern actually is as per the business logic or make observations by looking at the possible input strings, and then prepare the regex, otherwise, you may end up updating the expressions time and again.

Lastly, another option for you to use: “(?<=\b[A-Z]{2}\b\s)[\w-.,;_()]+$”

Do note, this regex assumes that the string to be extracted comes at the very end of the line. If something else can be there after the number to be extracted, remove the “$” character: (?<=\b[A-Z]{2}\b\s)[\w-.,;_()]+

@Tanmay_V_Chetule

System.Text.RegularExpressions.Regex.Match(“YourString”,“\d+-\d+”)

Hi @Tanmay_V_Chetule

Try this:
System.Text.RegularExpressions.Regex.Match(yourString, “\d{5}-\d{4}”).Value

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.