Home > Back-end >  Regex expression to ignore first and last character
Regex expression to ignore first and last character

Time:12-03

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.

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related