Home > Net >  Set a new value after using regex
Set a new value after using regex

Time:04-22

I have a string that is equal to stringnum.

stringnum = 0007001369920090687073

i need to use regex to grab the first 9 digits after the first 3 zeros. I am using this regex code "(?<=000)\d{9}" which gives me "700136992"

now that i have this, i need to make a variable named

result equal to the regexed version of stringnum

result = 700136992

I have tried

result =  scanline.match(/^.{3}\K.{9}/)

but it gives me

=> #<MatchData "700136992>

the number is what i need, but i dont need the #<matchdata stuff around it. How do i go about fixing this so that

result = "700136992"

if there is another way to set this value not using the .match method, i can do that as well. Im new to regex so im not sure the best way to set a new value after regexing

CodePudding user response:

You can use string[regexp, capture = 0] → new_string or nil:

When the Regexp argument regexp is given, and the capture argument is 0, returns the first matching substring found in self, or nil if none found:

Here is a short demo:

stringnum = '0007001369920090687073'
result = stringnum[/(?<=000)\d{9}/]
puts result
# => 700136992

See the Ruby demo online.

CodePudding user response:

Try this

p stringnum[/^.{3}\K.{9}/)/]

CodePudding user response:

What is wanted?

stringnum = "07010360009920090687"

Extract the nine digits following the first three digits

Why use a regular expression?

stringnum[3, 9]
  #=> "103600099"

See String#[].

Extract the nine digits following the first three zeroes

rgx = /\A(?:[1-9]*0){3}\K\d{9}/
stringnum[rgx]
  #=> "369920090"

Extract nine digits that follow three zeroes at the beginning of the string

rgx = /\A000\K\d{9}/
stringnum[rgx]
  #=> nil
"0000360009920090"[rgx]
   #=> "036000992"

(Similar to @Rajagopalan's answer.)

Extract the nine digits following the first three zeroes in a row

rgx = /(?<=000)\d{9}/
stringnum[rgx]
  #=> "992009068"

(@Wiktor's answer.)

Alternatively, rgx = /000\K\d{9}/.


Writing the regular expression /^(?:[1-9]*0){3}\K\d{9}/ in free-spacing mode makes it self-documenting.

/
\A          # match the beginning of the string
(?:         # begin non-capture group
  [1-9]*0   # match >= 0 chars the char class followed by '0'
){3}        # end the non-capture group and execute it 3 times
\K          # reset the beginning of the match and discard all 
            # previously-matched chars
\d{9}       # match 9 digits
/x          # invoke free-spacing regex definition mode

Similarly, /\A000\K\d{9}/ can be written:

/
\A          # match the beginning of the string
000         # match 3 '0'
\K          # reset the beginning of the match and discard all 
            # previously-matched chars
\d{9}       # match 9 digits
/x          # invoke free-spacing regex definition mode

/(?<=000)\d{9}/ and /000\K\d{9}/ can be written:

/
(?<=000)    # match '000' in a positive lookbehind
\d{9}       # match 9 digits
/x          # invoke free-spacing regex definition mode
/
000         # match '000'
\K          # reset the beginning of the match and discard all 
            # previously-matched chars
\d{9}       # match 9 digits
/x          # invoke free-spacing regex definition mode
  • Related