user4627270
user4627270

Reputation:

Matching Regex till a character

This is my string:

<address>tel+1234567890</address>

This is my regex:

([\d].*<)

which matches this:

1234567890<

but I dont want to match the last <character.

Upvotes: 0

Views: 51

Answers (2)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

You can use a positive lookahead:

\d+(?=<)

The (?=...) syntax makes sure what's inside the parens matches at that position, without moving the match cursor forward, thus without consuming the input string. It's also called a zero-width assertion.

By the way, the square brackets in [\d] are redundant, so you can omit them. Also, I've changed the regex, but perhaps you really meant to match this:

\d.*?(?=<)

This pattern matches everything between a digit and a <, including the digit. It makes use of an ungreedy quantifier (*?) to match up until the first < if there are several.

Upvotes: 2

m0bi5
m0bi5

Reputation: 9452

([\d]+)

This should work , try it out and let me know

Check the demo

Also as @LucasTrzesniewski said , you can use the look ahead

(\d+.(?=<))

Here is the demo

Upvotes: 1

Related Questions