Regex named capturing

  • Thread starter Thread starter Advait Mohan Raut
  • Start date Start date
A

Advait Mohan Raut

Hello friends,
I am new to Regex engine. I use Expresso to test RE. For a named
capturing test, I have following schema--

FullName --> first , surname OR
FullName --> surname , first , middle

eg. "advait raut" should return: first=advait surname=raut

and "raut advait mohan" should return: first=advait middle=mohan
surname=raut

For this I useed following RE.
^
(((<?first>[a-z]+)\s(<?surname>[a-z]+))|((<?surname>[a-z]+)\s(<?
first>[a-z]+)\s(<?middle>[a-z]+)))
$

But I dont get the valid match. Where am I mistaking ?

yours
Advait
 
Advait,

Assuming that Fullname could be written the way given below

FullName --> first , surname OR
FullName --> surname , first , middle

You could split the Fullname string separated by space.

If it returns 2 elements, it should be firstname, surname
If it returns 3 elements, it should have been surname, firstname,
middlename

Why bother using regexp for this?
Pardon me, if I don't understand your problem correctly.

Kalpesh
 
Kalpesh said:
Advait,

Assuming that Fullname could be written the way given below

FullName --> first , surname OR
FullName --> surname , first , middle

You could split the Fullname string separated by space.

Yes, I can use split but I will have to implement backtracking. It
would be better option if I have 4-5 clauses like here. But when I
have 20-30 clauses, I think Regex is better option. What do you
think ?
If it returns 2 elements, it should be firstname, surname
If it returns 3 elements, it should have been surname, firstname,
middlename

Yup, I also expect the same result but don't get it. Can anyone point
it out?
Why bother using regexp for this?
Pardon me, if I don't understand your problem correctly.

yours
Advait
 

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

Back
Top