Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Regex pattern to match the java part in JSP code

I am trying to match the content between <%.....%> in a JSP code. Suppose the sample JSP code is :

<%@ page import="java.io.*,java.util.*" %>
<html>
    <head>
        <title>Auto Refresh</title>
    </head>
    <body>
                <fieldset style="width:20%; background-color:#e6ffe6;">
                    <legend>Auto refresh</legend>
                    <h2>Auto Refresh Example</h2>
                    <%
                       // Set refresh, autoload time as 1 seconds
                       response.setIntHeader("Refresh", 1);
                       // Get current time
                       Calendar calendar = new GregorianCalendar();
                       String am_pm;
                       int hour = calendar.get(Calendar.HOUR);
                       int minute = calendar.get(Calendar.MINUTE);
                       int second = calendar.get(Calendar.SECOND);
                       if(calendar.get(Calendar.AM_PM) == 0)
                          am_pm = "AM";
                       else
                          am_pm = "PM";
                       String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
                       out.println("Crrent Time: " + CT + "\n");
                    %>
                </fieldset>
    </body>
</html>

My regex is /\<\%[\w \@\=\.\*\,\"\$\#\!\&\(\)\/\/\n\+\;\:]*\%\>/gi Its only matching the first part i.e

<%@ page import="java.io.*,java.util.*" %>

Not the second part

                    <%
                       // Set refresh, autoload time as 1 seconds
                       response.setIntHeader("Refresh", 1);
                      ........
                       String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
                       out.println("Crrent Time: " + CT + "\n");
                    %>
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

If you drop %> for trial you can see how far it matches. Until the backslash at \n" because it's not included in your specified characterset. Add the backslash or use something more general like

/<%[\s\S]*?%>/g

which will lazily match any amount of any character in between <% and %>.

See this demo at regex101

about 4 years ago · Santiago Gelvez Report

0

Try this regex. It's short and simple.

/<%.*?%>/gs

Demo

Explanation

.*? matches any character from zero to unlimited times, as few times as possible (lazy).

So, it takes everything between <% and closest %>. Pay attention to single line modifier /s

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!