0

I am trying to write a reg expression to find match of strings / code in a database.

here is some of the sample code / string which i need to remove using the regular expression.

[b:1wkvatkt] 
[/b:1wkvatkt]
[b:3qo0q63v]
[/b:3qo0q63v]
[b:2r2hso9d]
[/b:2r2hso9d]

Anything that match [b:********] and [/b:********]

Anybody please help me out. Thanks in advance.

4
  • 1
    What language are you using? What have you tried? What doesn't work? Commented Mar 26, 2017 at 12:08
  • 1
    If you're trying to do this with SQL then you'll have to tell what kind of database you use. Since regex stuff isn't part of standard ANSI SQL. Anyway, a regex to match those : \[\/?b:[a-z0-9]+\] Commented Mar 26, 2017 at 13:02
  • it works [\/?b:[a-z0-9]+] thanks ! Commented Mar 26, 2017 at 15:53
  • What do i need to do if i want to replace the pattern [b:********] and [/b:********] with <b> and </b> Commented Mar 26, 2017 at 15:56

1 Answer 1

1

You can use the following pattern (as stated by LukStorms in the comments):

\[\/?b:[a-z0-9]+\]

If you want to replace [b:********] with <b> (and also the closing one), you can use the following snippet (here in JavaScript, other languages are similar):

var regex = /\[(\/)?b:[a-z0-9]+\]/g;
var testText = "There was once a guy called [b:12a345]Peter[/b:12a345]. He was very old.";

var result = testText.replace(regex, "<$1b>");
console.log(result);

It matches an optional / and puts it into the first group ($1). This group can then be used in the replacement string. If the slash is not found, it won't be added, but if it is found, it will be added to <b>.

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.