Regular expression

hi,

I need to extract a string ‘MT/23232’ I have written the below code, but
it’s not working, any help me how to write the regular expression for
this,

msg=“MT/33235”
id = msg.scan(/MT/\d+/\d+/)[0]

puts id

Raja gopalan wrote in post #1165215:

I need to extract a string ‘MT/23232’ I have written the below code, but
it’s not working, any help me how to write the regular expression for
this,

msg=“MT/33235”
id = msg.scan(/MT/\d+/\d+/)[0]

Why do you use #scan if you only need the first match? In that case
String#[] is better.

Also, why are you matching two slashes if you only need to extract a
string with one?

What is your input and what is the format of the text you need to match?
Is it always four digits after the slash? Is there always “MT” before
the slash? Are you interested in the number only or do you need the
whole sequence starting with “MT/”?

hi Robert

Thanks,

yes I would use String#[].

yes it would always start with ‘MT’ and followed ‘/’ and any count of
numbers.

like

‘Policy created with MT/1212’

‘Policy created with MT/121212’

‘Policy created with MT/21212121212’

this is what I want.

Your regular expression is looking for two slashes instead of one, as
Robert pointed out.

/MT/\d+/

Yes I understood Joel. Thanks.