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.
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.
MA Value will also change in other cases… any other soln?
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.
Use the below regex expression
System.Text.RegularExpressions.Regex.Match(YourInput String Variable,"(\d+\-\d+)").Value
Hope it works!!
You can try this
[\d-]+$
System.Text.RegularExpressions.Regex.Match(str,"[\d-]+$").Value
Cheers
- 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!!
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-]+
Cheers
Steve
Also you can use the free website regexstorm to check your regex if you needed. The other members given to us a great solutions.
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-.,;_()]+
System.Text.RegularExpressions.Regex.Match(“YourString”,“\d+-\d+”)
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.