Home > Net >  How to find a string from a specific word to other word using rails
How to find a string from a specific word to other word using rails

Time:12-28

I have the following string and I'm trying to display the information from specific start word to specific end word:

Protocolo de medición
\r\n\r\nEnsayador disruptivo
\r\nDPA 75C Versión:  1.07
\r\nNúmero de serie: 1101908010
\r\n11/02/2022 02:15\r\n_____________________________
\r\n\r\nInformación sobre el ensayo
\r\n\r\nNombre de protocolo:        .......................
\r\nNúmero de muestra:      0569.1
\r\nMedición según norma:       ASTM D1816:2004 2mm
\r\nForma de electrodos:        Forma de seta
\r\nDistancia entre electrodos:         2 mm
\r\nFrec. del ensayo:   60 Hz\r\n\r\n_____________________________
\r\n\r\nConfig. según norma 
\r\n\r\nDiámetro de los electrodos:     36  mm\r\n\r\n_____________________________
\r\n\r\nValores de medición
\r\n\r\nTemperatura:        20 °C
\r\n\r\nMedición 1:         60.6  kV
\r\nMedición 2:         72.7  kV\r\nMedición 3:         >75.0  kV
\r\nMedición 4:         54.7  kV\r\nMedición 5:         66.4  kV
\r\n\r\nValor medio:                65.9  kV
\r\nDesviación estándar:            8.4  kV
\r\nDesviación estándar/val. medio: 12.8  %
\r\n\r\n\r\nEnsayo correctamente realiz.
\r\n\r\n\r\nEnsayo ejecutado por:   .......................

The code should find the string line

\r\nNúmero de muestra:      0569.1 \r\

Final result should be

0569.1

I tried this code only display the word searched

@article.description.match(/Número de muestra:\b/)

I tried this code and works but i need to count the number from and to

<%=  @article.description.slice(249..260) %>

What i want is write the FROM WORD - TO WORD string without typing the index word.

CodePudding user response:

If the string you are looking to capture always has a line end character after it at some point you can do:

data = @article.description.match(/Número de muestra:*(.*)$/)

returns a Match object like:

#<MatchData "Número de muestra:      0569.1" 1:"0569.1">

you can then access the match with

data[1]
# => "0569.1"

The Match object stores the matching string in data[0] and the first capture is in data[1]. In the regexp we are using the .* matches the spaces after the string Número de muestra:. The (.*) matches any characters after the spaces. The $ matches the end of line character. Anything that matches what is between the parens () gets stored as matches in the Match object.

  • Related