skip to Main Content

I need to bring a text to html tag, like this:

input: p1: Question 1

output: <h3>Question 1</h3>

or also

input: question 1: ¿question 1?

output: <h3>¿question 1?</h3>

The detail that I don’t get it, I have the following Regex rule.

([a-zA-Z])([1-9])(:)+(.*)?

and my result is:

<h3> Question 1</h3>
question 1: ¿question 1?
  • In the first one I need to remove the space that is generated between <h3> and the Q
  • In the second it doesn’t work for me at all

Could you help me in what I am failing in the regex rule?

DEMO

3

Answers


  1. Chosen as BEST ANSWER

    The answer was, thanks to the user @User863

    ([a-zA-Z ])+([1-9])(:)+s*(.*)
    

  2. The regexp needs to match optional whitespace before the question number (to fix the second problem) and after the : (to fix the first problem).

    ([a-zA-Z]+)s*([1-9])(:)+s*(.*)
    

    DEMO

    There’s no need for ? after (.*). Since .* matches a zero-length string, you don’t need to mark it as optional.

    I’m not really sure why you have + after (:) — do you really want to allow something like Question 1:::::?

    Login or Signup to reply.
  3. You can use a single capture group, and match optional horizontal whitespace chars with h*

    In the capture group, match at least a single non whitespace char with S

    [a-zA-Z]+h*[1-9]:h*(S.*)
    

    Regex demo

    In the replacement use the value of group 1:

    <h3>$1</h3>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search