Substring from regexp

  • Thread starter Eduardo Yáñez Parareda
  • Start date
E

Eduardo Yáñez Parareda

Hi all,

how could I get a substring which match a regular expression?

I mean:

"assddHellOasddaffer".something(/^H.*O$) => HellO
 
M

Marcin Mielżyński

Eduardo said:
Hi all,

how could I get a substring which match a regular expression?

I mean:

"assddHellOasddaffer".something(/^H.*O$) => HellO

"assddHellOasddaffer"[/H.*O/]

lopex
 
E

Eduardo Yáñez Parareda

"assddHellOasddaffer"[/H.*O/]

Thanks, I'm newbie with Ruby but with regexps too!

Well, another question. Having this string


"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f"

if I split it with /1\{.*-\}/ I get:

["fsdfadfdsf", "f"]

that's right, but I want to get ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"] that's said, the inverse of split.
Might I change the regexp or is there any method I could use?
 
E

Eduardo Yáñez Parareda

"assddHellOasddaffer".match(/(H.+O)/)
puts $1

The parenthesis form groups for back-referencing. You don't want the ^
or $ in your Regexp because the pattern you're looking for isn't at the
beginning or end of a string.

Yes, I realized that after reading Marcin's answer...
 
R

Robert Klemme

Eduardo said:
"assddHellOasddaffer"[/H.*O/]

Thanks, I'm newbie with Ruby but with regexps too!

Well, another question. Having this string


"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f"

if I split it with /1\{.*-\}/ I get:

["fsdfadfdsf", "f"]

that's right,

Sure? I'd rather guess that you want three strings.
but I want to get ["{1dffsdfadsf-}",
"{1fsdfsdfsdfsdfsdh-}"] that's said, the inverse of split.
Might I change the regexp or is there any method I could use?

You want #scan:

irb(main):003:0>
"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f".scan /\{[^}]*\}/
=> ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"]
irb(main):004:0>
"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f".scan /\{.*?\}/
=> ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"]
irb(main):005:0>
"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f".scan /\{[^}]*?\}/
=> ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"]

Kind regards

robert


PS: It's usually better to post new questions to new threads.
 
E

Eduardo Yáñez Parareda

You want #scan:

Yes!, I tried it before post this message but may be with wrong regexp :(
PS: It's usually better to post new questions to new threads.

I'm sorry , better next time. Thanks a lot.
 
E

Eduardo Yáñez Parareda

"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f".scan /\{.*?\}/
=> ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"]

I think I'm bit tedious but why it doesn't work if I put some \n inside source string?
Is '\n' character not considered when using '.*' expression?
 
M

Marcin Mielżyński

Eduardo said:
"fsdfadfdsf{1dffsdfadsf-}fsdfdsfa{1fsdfsdfsdfsdfsdh-}f".scan /\{.*?\}/
=> ["{1dffsdfadsf-}", "{1fsdfsdfsdfsdfsdh-}"]

I think I'm bit tedious but why it doesn't work if I put some \n inside
source string?
Is '\n' character not considered when using '.*' expression?

use multiline option:

/.../m

lopex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top