1

I have been banging my head with this for the last hour and a half and cannot make it work. I am trying to create a regular expression for MemoQ's Regex tagger.

I have a series of these:

zzz [xxx]yyy[/xxx] zzz

or

zzz [xxx]yyy[/xxx]

or

[xxx]yyy[/xxx] zzz

or

[xxx]yyy[/xxx]

For example:

TEXT TEXT TEXT [img height=32]logo_small.dds[/img] TEXT TEXT TEXT

I want to transform into a single tag everything from the first open parenthesis to the last closed one, which is:

[img height=32]logo_small.dds[/img]

Please remember that the expression should work regardless of what is inside the parentheses and in between the two. Also please note that could be other text before and after that should not be converted.

You can find the rules for Regex tagger here (should be standard, anyway): https://docs.memoq.com/current/en/Places/regular-expressions.html

I have tried several expressions, the last one being this: \[(.*?)\](.*?)\[(.*?)\]

Thank you for your help and have a great day!

7
  • 1
    This seems to be BBCode. What language/platform/framework/tool are you using? Commented Jan 30, 2024 at 16:56
  • First of all, thank you. MemoQ Regex tagger. docs.memoq.com/current/en/Places/regular-expressions.html Commented Jan 30, 2024 at 17:27
  • 3
    If you are answering @InSync's question, then please edit that answer into the question. Clarifications to the question should not be in comments. Commented Jan 30, 2024 at 17:53
  • 2
    ...and while you are at it, please also specify if you need to deal with nested tags and/or any edge cases. Commented Jan 30, 2024 at 20:20
  • I am very sorry, but I do not know what those are. Commented Jan 31, 2024 at 18:05

1 Answer 1

1

The solutions below do not match nested tags, this is because OP does not require matching nested tags.

If you just need to match strings like [...]...[...], you can use

\[[^][]*].*?\[[^][]*]

See the regex demo. Details:

  • \[ - a [ char
  • [^][]* - zero or more chars other than [ and ]
  • ] - a ] char
  • .*? - any zero or more chars other than line break chars as few as possible
  • \[[^][]*] - a [ char + zero or more chars other than [ and ] + ] char.

A bit more comprehensive expression - that would only match the paired tags - would look like

\[(\w+)\b[^][]*][\w\W]*?\[/\1\b[^][]*]

See this regex demo. This matches

  • \[ - a [
  • (\w+) - group 1: one or more word chars
  • \b - word boundary
  • [^][]* - zero or more chars other than [ and ] chars
  • ] - a ] char
  • [\w\W]*? - any zero or more chars as few as possible
  • \[/ - a [/ text
  • \1 - same text as captured into Group 1
  • \b - a word boundary
  • [^][]*] - zero or more chars other than [ and ] chars + ] char.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.