mvee
mvee

Reputation: 11

Regex match word between special characters

I have need to get the word between . and )

I am using this: \..*\)

But if there is more than one . I am getting the first . to ) instead of the last . to ).

E.g:

abc.def.ghi.jkl.mymodname(Object sender, CompletedEventArgs e)

I am getting :

def.ghi.jkl.mymodname(Object sender, CompletedEventArgs e)

I want:

mymodname(Object sender, CompletedEventArgs e)

Any pointers? I'm new to regex, as you can see...

Upvotes: 0

Views: 1930

Answers (4)

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

Be explicit. If you don't want any dots between your starting dot and the closing parenthesis, you can specify this:

\.[^.]*\)

[^.] is a negated character class, meaning "any character except a dot". That way, only non-dots are allowed to match after the dot.

And if you don't want the leading dot to be a part of the match (but do want it to be present before the start of the match), you can use a lookbehind assertion:

(?<=\.)[^.]*\)

This works in nearly all regex engines except for JavaScript and Ruby (until version 1.8), both of which do not support lookbehind.

Upvotes: 3

ghoti
ghoti

Reputation: 46856

It sounds like what you really want to do is strip off everything to the last dot.

In shell:

    sed 's/.*\.//'

or in PHP

    preg_replace('/^.*\./', '', $foo);

Beware of stray dots within your method, though.

def.ghi.jkl.mymodname(Object sender, "Some text.", CompletedEventArgs e)

For this, you might want:

    sed -r 's/.*\.([a-zA-Z]\()/\1/'

or something equivalent.

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65274

in PHP

$a='abc.def.ghi.jkl.mymodname(Object sender, CompletedEventArgs e)';
$p='/.*\.(.*?\))/';
preg_match($p,$a,$m);

now look at $m[1]

Other languages analogous

Upvotes: 0

udnisap
udnisap

Reputation: 909

try .[a-bA-B0-9,(]*)
I did not check it but I think something like that works or you can also use somthing

Upvotes: 0

Related Questions