简体   繁体   中英

Regex match everything after a certain character (b) except a another character (closing curly brace)

Problem:

I have a string, {aabb.1234} - those curly brackets are included. I'm trying to make a regex to match everything after the second b except that last closed curly bracket } . In other words, I need to match a period and everything after it.

What I've tried:

So far I have the first part, which identifies everything after the second b : \.[^.]+$ , but I'm at a loss on how to make the } an exception. Thoughts?

Edit: I'm planning to use this regex inside R 's gsub function. If that makes a difference in your suggestions.

Match the last curly bracket with a non capturing group and what you want with a normal group. You can then access your desired string with group #1 of each match.

(\.[^.]+)(?:})$

here is a working example

Edit per the question update:

It works with R, as well, note group #1 of the match. See here at regexr.

在此处输入图像描述

You may use the following regex pattern:

(?<=[^b]b).*?(?=\}|$)

Demo

Here is an explanation of the regex pattern:

(?<=[^b]b)  assert that what precedes is the first b (itself preceded by a non b)
.*?         then match all content until reaching
(?=\}|$)    a } or the end of the input

You need a sub like

sub('.*\\.([^}]+).*', '\\1', '{aabb.1234}')

See the R online demo and the regex demo . Note that gsub replaces all occurrences, while sub replaces the first one, and sub is enough since here, the whole string is matched, and replaced with a backreference to Group 1.

Details :

  • .* - zero or more chars, as many as possible
  • \. - a dot (it is double escaped in the R string literal, as \ is used to form string escape sequences, such as "\n" (a line feed char, etc.)
  • ([^}]+) - Group 1: any one or more chars other than a } char ( [^...] is a negated bracket expression in a TRE regex)
  • .* - zero or more chars, as many as possible (the rest of the string)

The \1 in the replacement refers to the value of Group 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM