regular expression

P

Petr Dupovnik

[Note: parts of this message were removed to make it a legal post.]

Hello

Suppose I have a string with some repeating patterns:

string = "some miscellaneous text [sdfsdf.wer], some more miscellaneous text
[vbnfg.thy], and yet more text [jkhjkhjk.345]"

I want catch all instances of "[.*]" in this line - without the square
brackets. - in the above example that would be 'sdfsdf.wer', 'vbnfg.thy',
and 'jkhjkhjk.345'.

What regular expression would pull each instance of "[.*]" into a separate
element in an array?

my_match=string.match('\[(\w+\.\w{3}\]')

This only catches the first match, and ignores the second and third.

grateful for any help.

Petr.
 
S

Sebastian Hungerecker

Petr said:
What regular expression would pull each instance of "[.*]" into a separate
element in an array?

"some miscellaneous text [sdfsdf.wer], some more miscellaneous text
[vbnfg.thy], and yet more text [jkhjkhjk.345]".scan(/\[[^\]]+\]/).flatten
=> ["[sdfsdf.wer]", "[vbnfg.thy]", "[jkhjkhjk.345]"]

HTH,
Sebastian
 
K

Kyle Schmitt

Hello

Suppose I have a string with some repeating patterns:

string = "some miscellaneous text [sdfsdf.wer], some more miscellaneous text
[vbnfg.thy], and yet more text [jkhjkhjk.345]"

I want catch all instances of "[.*]" in this line - without the square
brackets. - in the above example that would be 'sdfsdf.wer', 'vbnfg.thy',
and 'jkhjkhjk.345'.
string.scan /(\[[^\]]*\])/
would do the trick
..although you could also use split with that same regex if you needed
the rest of the data for something.

scan is only keeping the saved part of the regex, as marked by our parenthesies
Inside of them is the slightly ugly statement
\[[^\]*]\]
since brackets are special in regexs, we have to escape them first,
hence the \[ and \] stuff
So it matches one bracket, [, and anything that isn't another bracket,
], followed by one bracket, ].

Does that make sense?

--Kyle
 
P

Petr Dupovnik

[Note: parts of this message were removed to make it a legal post.]

Perfect.

Thanks Kyle & Sebastian.

Hello

Suppose I have a string with some repeating patterns:

string = "some miscellaneous text [sdfsdf.wer], some more miscellaneous text
[vbnfg.thy], and yet more text [jkhjkhjk.345]"

I want catch all instances of "[.*]" in this line - without the square
brackets. - in the above example that would be 'sdfsdf.wer', 'vbnfg.thy',
and 'jkhjkhjk.345'.
string.scan /(\[[^\]]*\])/
would do the trick
..although you could also use split with that same regex if you needed
the rest of the data for something.

scan is only keeping the saved part of the regex, as marked by our
parenthesies
Inside of them is the slightly ugly statement
\[[^\]*]\]
since brackets are special in regexs, we have to escape them first,
hence the \[ and \] stuff
So it matches one bracket, [, and anything that isn't another bracket,
], followed by one bracket, ].

Does that make sense?

--Kyle
 
R

Robert Dober

Petr said:
What regular expression would pull each instance of "[.*]" into a separate
element in an array?

"some miscellaneous text [sdfsdf.wer], some more miscellaneous text
[vbnfg.thy], and yet more text [jkhjkhjk.345]".scan(/\[[^\]]+\]/).flatten
=> ["[sdfsdf.wer]", "[vbnfg.thy]", "[jkhjkhjk.345]"]

HTH,
Sebastian


scan( /\[(.*?)\]/ ).flatten

is a shorter alternative showing the use of the non greedy Kleene Star "*?"
But often it is indeed a good idea to be very explicit in your regular
expressions.

Cheers
Robert
 
K

Kyle Schmitt

But often it is indeed a good idea to be very explicit in your regular
expressions.

Often it's a good idea just because it keeps you from using explicit
language when debugging your code later....
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top