Regex position error

I have a regular expression:
Regexp.new(/^(\w+([-']\w+)*)(\ \w+([-']\w+)*)+$/)
and I need to know the position of a invalid character.

For example, if a invalid character is a number, I can recover it using:
"gisele bund9chen".index(/\d/)

But, I need generalize the regular expression to find any a character
different of \w, space \' and \-.

Thanks,
Carlos Camargo

···

--
Posted via http://www.ruby-forum.com/.

I have a regular expression:
Regexp.new(/^(\w+([-']\w+)*)(\ \w+([-']\w+)*)+$/)

Regexp.new(regexp) is the same as regexp. Don't wrap in Regexp.new for no reason.

and I need to know the position of a invalid character.

For example, if a invalid character is a number, I can recover it using:
"gisele bund9chen".index(/\d/)

/\d/ is to /[0-9]/ as /\D/ is to /[^0-9]/

But, I need generalize the regular expression to find any a character
different of \w, space \' and \-.

For more goodies see the regexp section of the ruby quickref.

···

On Aug 10, 2011, at 15:19 , Carlos Camargo wrote:

Hi, De Luca and Davis,

Thanks for your help. I found a solution that works fine:
/( |-|')( |-|')|^( |-|')|( |-|')$|^$|\d|[^a-z
çáäãàâéèëêíìïöóòôõüúùû\-\']/i
The solution proposed by De Luca doesn't work for some characters (e.g.
"?").

Best regards,
Carlos Camargo

···

--
Posted via http://www.ruby-forum.com/.

str = %Q[gisele !'_"9? bundchen]

str.scan(/[^\w' -]/) do |match|
  puts match
end

--output:--
!
"
?

···

--
Posted via http://www.ruby-forum.com/.

Whoops.

I need to know the position of a invalid character.

str = %Q[gisele !'_"9? bundchen]

str.scan(/[^\w' -]/) do |match|
  md = Regexp.last_match
  pos = md.begin(0)
  puts "#{match} : #{pos}"
end

--output:--
! : 7
" : 10
? : 12

···

--
Posted via http://www.ruby-forum.com/\.

Dear Camargo,

/((^| )[ -']|[^\D'- ]/ or something like this

Try to follow the KISS principle. As a rule of thumb,
when something is getting too much complex, you might be doing it wrong.

Regards,

···

---
     Luiz Angelo Daros de Luca, Me.
            luizluca@gmail.com

2011/8/10 Ryan Davis <ryand-ruby@zenspider.com>

On Aug 10, 2011, at 15:19 , Carlos Camargo wrote:

> I have a regular expression:
> Regexp.new(/^(\w+([-']\w+)*)(\ \w+([-']\w+)*)+$/)

Regexp.new(regexp) is the same as regexp. Don't wrap in Regexp.new for no
reason.

> and I need to know the position of a invalid character.
>
> For example, if a invalid character is a number, I can recover it using:
> "gisele bund9chen".index(/\d/)

/\d/ is to /[0-9]/ as /\D/ is to /[^0-9]/

> But, I need generalize the regular expression to find any a character
> different of \w, space \' and \-.

For more goodies see the regexp section of the ruby quickref.