Risha Parveen
Risha Parveen

Reputation: 1

Regex expression to ignore first and last character

So I am trying to make a regex match for strings of the form: "catalog.schema.'tablename'" .

The output I am looking for is just catalog.schema.'tablename' leaving out the quotes at the end position.

Can anyone help me out

I tried to do it with the expression /(?!^|.$)+[^\s]/ which leaves out the end quotes but matches each character.

So I modified it to /(?!^|.$)+[^\s]+/g . This matches the whole sentence but doesn't ignore the end quote.

Upvotes: 0

Views: 570

Answers (3)

The fourth bird
The fourth bird

Reputation: 163362

If you don't want to match the first and the last character, you can just use a capture group instead of lookarounds and use the group 1 value.

The first . matches the first of (any) characters, the (.+) is a capture group that matches 1 or more characters, and the . at the end matches the last character of the string.

.(.+).

Regex demo

Or to get the text between the double quotes at the start and the end of the string using a negated character class and a capture group:

^"([^"]+)"$

Regex demo

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

You can just use

(?<=.).+(?=.)

Or, if you cannot use lookbehind:

(?!^).+(?!$)

See the regex demo #1 and regex demo #2.

Since . matches any char other than line break chars, the patterns just match any strings without their start and end chars.

Upvotes: 1

An-dir
An-dir

Reputation: 465

Depends on the data arround your string and quotationmarks may be within the string.

Why not just this: "(.*?)"

https://regex101.com/r/oaS8o0/1

To answer the question in the title you might simply use: ^.(.*)?.$

https://regex101.com/r/FxJgtW/1

Upvotes: 1

Related Questions