Regex for url pattern validation

Hi,
I am trying to find a regex for Url pattern validation that is not too restrictive.
Please let me know, if anyone is using a pattern for their applications.
Thanks
Mayank Sharma

are you talking about xml schema restriction?
<xs:element name="url">
  <xs:simpleType>
   <xs:restriction base="xs:string">
    <xs:pattern value="(https?://)?[-\w.]+(:\d{2,5})?(/([\w/_.]*)?)?" />
   </xs:restriction>
  </xs:simpleType>
</xs:element>

Similar Messages

  • Regex for URL Validation

    HI I am validating a field which takes a url as input from the user.So the url may contain http,https,ftp links or the user may specify the ip address.So can anyone please help me out to create a suitable regex for that.I just need to check the validness of link and not that it exists or not.

    here i have changed the regex as suggested by you but
    it returns true even for http://com.com@
    which is invalid.
    So it did not worked :-(It did not work, because you did not use it correct:
    public class Foo {
        private static boolean isValidUrl(String url) {
            return url.matches("(https?|ftp)://www\\.\\w+\\.\\w+");
        public static void main(String[] args) {
            String[] urls = {
                    "http://com.com@",
                    "http://www.com.com",
                    "http://www.com.nl",
                    "http://www.com.co.uk",
                    "http://www.com"
            for(String url : urls) {
                System.out.println(url+" valid? "+isValidUrl(url));
    }

  • Regex for URL Expression

    I am not a programmer, but I have to write a rule for CACHING the following URL that is being generated with "GET_QUERYSTRING".
    /myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map&mx=198&my=247&request=gettile&zoomlevel=3
    As you can see, I would like to cache all the PNG map tiles, but I don't know how to come up with Regular Expression (regex) for the above URL.
    Thanks,

    are you talking about xml schema restriction?
    <xs:element name="url">
      <xs:simpleType>
       <xs:restriction base="xs:string">
        <xs:pattern value="(https?://)?[-\w.]+(:\d{2,5})?(/([\w/_.]*)?)?" />
       </xs:restriction>
      </xs:simpleType>
    </xs:element>

  • How to use a wild card for url pattern in web.xml?

              I want to use web container managed security to protect all my JSP pages. I tried
              to use the <security-constraint> tag in web.xml and set <url-pattern> to /*.
              However, none of my pages is protected. Specifying each page individually works,
              but I really don't want to do that. Any ideas?
              

    Eric:
              Which version of WebLogic Server are you using (including any service pack or rolling
              patches)?
              Eric Ma wrote:
              > I want to use web container managed security to protect all my JSP pages. I tried
              > to use the <security-constraint> tag in web.xml and set <url-pattern> to /*.
              > However, none of my pages is protected. Specifying each page individually works,
              > but I really don't want to do that. Any ideas?
              Jim Brown
              Developer Relations Engineer
              BEA Support
              

  • ***Need Regex for a pattern

    Hello,
    I need a pattern for the following use cases: I am using Java
    1. var so = nokia.device.load('abc','abc');
    2. var so = nokia.device.load("abc","abc");
    3. var so = nokia.device.load("abc");
    4. var so = nokia.device.load('abc');
    // 5. var so = nokia.device.load('abc'); (Line is commentted)+
    function init()
    if (window.menu) {
    // set tab-navigation mode and hide softkeys
    widget.setNavigationEnabled(false);
    var so = nokia.device.load("abc");(Line is commentted)+
    I need a regex pattern which will be give me only the function statments which are listed in bold and NOT italic format function statements as those statements are commented.
    Basically, the pattern should give me the statments which are not commented.
    Please help me, its urgent.
    Thanks,
    Mallikarjun.
    Edited by: arjun2010 on Apr 26, 2010 9:36 PM

    I'll let the fact that the requirement is very dodgy slide for a moment. It seems that a pattern might not be the solution here; why not do something like the following:
    SWEEP 1:
    - scan through the code one character at a time, looking for comment blocks and comment lines.
    - remove all comment blocks from the code (or rather, copy all parts that are not comments to a new buffer)
    now you have code cleaned of all comments
    SWEEP 2:
    - scan for 'noka.device.load' and extract those pieces of code with some clever string comparisons.
    Of course I don't know the why behind this requirement, but if I had to implement such a crude requirement I'd do it as simple as possible in stead of as clean as possible.

  • Problem using regex for url

    Heres the problem im trying to match
    <b>Heres my text</b>
    and using this pattern
    Pattern rnpattern = Pattern.compile(".+rel-.+html.+id=.+>(.+)</a>.+");
    and I'm having to matches returned at all. Its actually pulling a full text file and running the pattern on a CharBuffer, but I dont think thats the issue. I have 2 other patterns being run the same way in the same file and theres no problem with them. I know the pattern is not optimized at all, Ive gone through about 15 different iterations of trying different combinations to try and get a match, and this is the last one I tried. the numbers in the url such as this one being 3533 change so that cant be static. And the text such as "Heres my text" changes and thats what I want to capture. If anyone could lend any assistance I would appreciate it.
    Chris N.

    http://www.foad.org/~abigail/Perl/url2.html

  • Different url-patterns for same servlet running in 2 different environments

    Hi All,
    I have a question about url mappings in my web.xml file and I hope somebody can help. The situation is that I�m putting together a web app using a combination of JSPs and servlets. I can�t develop on the machine that it will be hosted on, so I�m working and testing on my own machine and will transfer to the host machine when finished. However, the host machine is set up to map serlvets to http://�/servlet/MyServlet but the copy of tomcat I have installed locally maps to http://�/MySerlvet. My question is, when I get ready to transfer my application do I have to go through all my code and find serlvet references and insert the �servlet/� path info required by the hosting service, or can I just change the url patterns for the servlet mappings of the web.xml file on the host machine ? In other words:
    Local install of Tomcat where servlets are accessed at http://�/MyServlet
    <servlet-mapping>
    <servlet-name> MyServlet </servlet-name>
    <url-pattern>/MyServlet </url-pattern>
    </servlet-mapping>
    Host machine install of Tomcat where servlets are accessed at http://�/servlet/MyServlet
    <servlet-mapping>
    <servlet-name> MyServlet </servlet-name>
    <url-pattern>/servlet/MyServlet </url-pattern>
    </servlet-mapping>
    I guess my thinking is that it would be better/easier to have a remote and local version of the web.xml file that reflects the environment each one resides in and have only one codebase rather than 2 codebases and 1 web.xml file. Am I thinking about this in the right way, or have I misunderstood something?
    Thanks,
    Peter

    What you�re suggesting is logical, but won�t that
    effect all the other stuff I�ve got running on my
    local Tomcat install that expects servlets to be
    accessed at http://.../ServletName ?
    in web.xml, you decide of your mapping, so you could use the /servlet/ServletName pattern for your application that needs to be remote, and /ServletName for the rest of your stuff. You can even define more than one mapping for a servlet...
    In fact, you should probably put your whole application that is going to be on a remote server in it's own context, and to be ahead of dufymo :-) , learn to put it a war file for deployment.

  • Regex for a URL

    How do I write a regex for the following URL. I am trying to write a regex rule in Oracle WebCache.
    /myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map&mx=198&my=247&request=gettile&zoomlevel=3
    Thanks,

    I would start with a not so restrictiv expression.
    something like
    SQL> with testdata as (select '/myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map&mx=198&my=247&request=gettile&zoomlevel=3' urlstring from dual union all
      2                    select '/myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map&mx=200&my=300&request=gettile&zoomlevel=180' urlstring from dual union all
      3                    select '/myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map&my=300&mx=200&request=gettile&zoomlevel=0' urlstring from dual union all
      4                    select '/myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map&mx=100&my=200&request=gettile' urlstring from dual union all
      5                    select 'somethingelse' urlstring from dual)
      6  select urlstring
      7  from testdata
      8  where regexp_like(urlstring,'^/myserver.domain.com:7779/mapviewer/mcserver[?]format=[PNG|SVG].+[mapcache=mvdemo.demo_map].+[request=gettile]');
    URLSTRING
    /myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map
    &mx=198&my=247&request=gettile&zoomlevel=3
    /myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map
    &mx=200&my=300&request=gettile&zoomlevel=180
    /myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map
    &my=300&mx=200&request=gettile&zoomlevel=0
    /myserver.domain.com:7779/mapviewer/mcserver?format=PNG&mapcache=mvdemo.demo_map
    &mx=100&my=200&request=gettile
    SQL> In this example the url needs to include the mapcache, format and request parameter.
    Edited by: Sven W. on Oct 29, 2008 7:23 PM

  • Obtaining url patterns for a user Role/Group

    I am looking for a way to find out how to obtain a the list of accessible urls (url
    patterns) for a role/group defined in weblogic.
    I have gotton as far as if checking if a user is in a group using: javax.ejb.SessionContext
    isCallerInRole(java.lang.String roleName)
    After checking if a user is in a role/group I want to access the url pattern authorised
    for a particular role under weblogic.
    Can this be done.

    I am looking for a way to find out how to obtain a the list of accessible urls (url
    patterns) for a role/group defined in weblogic.
    I have gotton as far as if checking if a user is in a group using: javax.ejb.SessionContext
    isCallerInRole(java.lang.String roleName)
    After checking if a user is in a role/group I want to access the url pattern authorised
    for a particular role under weblogic.
    Can this be done.

  • url-pattern for extension mapping in security-constraint not working

    I'm trying to use extension mapping in a <security-constraint> configuration,
    According to:
    http://download.oracle.com/otn-pub/jcp/servlet-3_1-fr-eval-spec/servlet-3_1-final.pdf?AuthParam=1429824454_de04222eab1b8…
    Section 12.2:
    A string beginning with a ‘*.’ prefix is used as an extension mapping.
    But WebLogic does not take in consideration my configuration. If I use path mapping exact mapping it work.
    My configuration is:
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Unsecured</web-resource-name>
            <url-pattern>*.wsdl</url-pattern>
            <url-pattern>*.xsd</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>HttpAuth</web-resource-name>
            <url-pattern>/ws/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>ws-user</role-name>
        </auth-constraint>
        <user-data-constraint>
            <transport-guarantee>INTEGRAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>Test1</realm-name>
    </login-config>
    <security-role>
        <role-name>ws-user</role-name>
    </security-role>
    WebLogic Server 12c (12.1.3)
    Has anybody used extension mapping with security-constraint? Is that a WebLogic issue?

    Hi nikita,
    I have delt with the same problem before. As you say, most JSF actions all get posted back to the original page, and the faces servlet internally redirects according to the navigation rules and actions. This can mean the URL seen by the browser does not always correspond to the actual JSP (wrapped by JSF) that produced the content.
    Generally adding the "<redirect/>" tag to all your navigation rules (in faces-config.xml) remedies this, so the actions are still posted back to the original page, but then the JSF servlet sends an http-redirect to the browser before invoking the new page. This way, the URL is always in sync, and the security constraints defined in your web descriptor always get invoked properly.
    regards,
    tony

  • Regular expression in url-pattern in web.xml

    hi,
    could any tell me the is web.xml accepts regexp(except wild card) in it's url-pattern attribute. I want to put regular expression (except wildcard, i know it supports) as [a-zA-Z]+ like in url-pattern in servlet-mapping. I tried this :
    <servlet-mapping>
    <servlet-name>AppDnldMain</servlet-name>
    <url-pattern>/[a-zA-Z]+/?</url-pattern>
    </servlet-mapping>
    But it dint work.. I dont know whether i processed in right way. Any suggestion are most welcome..

    No it does not accept wild card.
    Servlet specification section SRV.11.2
    In theWeb application deployment descriptor, the following syntax is used to define
    mappings:
    • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used
    for path mapping.
    • A string beginning with a ‘*.’ prefix is used as an extension mapping.
    • A string containing only the ’/’ character indicates the "default" servlet of
    the application. In this case the servlet path is the request URI minus the context
    path and the path info is null.
    • All other strings are used for exact matches only.
    If you want regex matching, you can match all urls to a single servlet, and then use request.getRequestURI and do regex on that in your code.
    Cheers,
    evnafets

  • Regex for international character sets like [a-zA-Z] + �, Ł, �, etc.

    Hi all!
    Is there an internationalized equivalent for the pattern "[a-zA-Z]+" which would accept the French, German, Polish or other local characters like "&#321;" or "�" or "�"?
    It's a bad idea to add all of them to the standard regex like "[a-zA-Z���&#321;��]+", since you never know what comes next.
    I tried to use the "\\p{Alpha}+" to check the string "&#321;ukasz", but the string didn't match. Is there any other possibility other than to exclude "=", ">", "+", "�", etc.?
    Thanks for any help!
    Lena

    My application will get the UTF-8 encoded string and
    has to ensure it represents a valid name.There is no such thing as a "utf-8 encoded string." In Java strings are internally encoded in one of the 16-bit tranformation formats but you don't need to know this most of the time.
    The code you post works accidentally. It does not work in the general case. Just do what I said in my previous post.
    Try this code for instance:System.out.println(Charset.forName("UTF-8").encode(">8<>")
                         .asCharBuffer().toString().matches("\\p{L}+"));It will print "true" because the resulting charbuffer does match the pattern even though the original string does not. To understand why this happens I recommend reading the API documentation of the relevant classes.

  • Filter does not work with *.jsp URL pattern???

    Hi All,
    I am, by no means, very good at JSF or Java. I have looked at various forum posts on here for ways to implement a security filter to intercept requests to pages that first require one to be logged in, and if not, redirect them to the login page. Yes, I know a lot of you have heard this many times before, and I'm sorry to bring it up again.
    BUT, from the guidance of other posts, I have got a filter that works fine when the url pattern is set to "/faces/*" or "/<anything>/*", however it won't work for "*.jsp" or "*.<anything>"
    My filter is as follows:
    package test.security;
    import javax.faces.context.FacesContext;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.http.HttpSession;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class SecurityFilter implements Filter{
        /** Creates a new instance of SecurityFilter */
        private final static String FILTER_APPLIED = "_security_filter_applied";
        public SecurityFilter() {
        public void init(FilterConfig filterConfig) {
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException{
            HttpServletRequest req = (HttpServletRequest)request;
            HttpServletResponse res = (HttpServletResponse)response;
            HttpSession session = req.getSession();
            String requestedPage = req.getPathTranslated();
            String user=null;
            if(request.getAttribute(FILTER_APPLIED) == null) {
                //check if the page requested is the login page or register page
                if((!requestedPage.endsWith("Page1.jsp")) /* This is the login page */
                    //set the FILTER_APPLIED attribute to true
                    request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
                    //Check that the session bean is not null and get the session bean property username.
                    if(((test.SessionBean1)session.getAttribute("SessionBean1"))!=null) {
                        user = ((test.SessionBean1)session.getAttribute("SessionBean1")).getUsername();
                    if((user==null)||(user.equals(""))) {
                       // try {
                     //       FacesContext.getCurrentInstance().getExternalContext().redirect("Page1.jsp");
                      //  } catch (ServletException ex) {
                      //      log("Error Description", ex);
                        res.sendRedirect("../Page1.jsp");
                        return;
            //deliver request to next filter
            chain.doFilter(request, response);
        public void destroy(){
    }My web.xml declaration for the filter is:
    <filter>
      <description>Filter to check whether user is logged in.</description>
      <filter-name>SecurityFilter</filter-name>
      <filter-class>test.security</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>SecurityFilter</filter-name>
      <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    Note: I have also tried this with <url-pattern>*.jsp</url-pattern> for the filter mapping in place of the Faces Servlet
    My web.xml declaration for the url pattern is:
    <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.jsp</url-pattern>
    </servlet-mapping>Which JSC/NetbeansVWP automatically creates a "JSCreator_index.jsp" which has:
    <?xml version="1.0" encoding="UTF-8"?>
    <jsp:root  version="1.2" xmlns:jsp="http://java.sun.com/JSP/Page">
      <jsp:forward page="Page1.jsp"/>
    </jsp:root>When run, this causes an Error 500 in the browser and a NullPointerException in SecurityFilter.java on the line:
    if((!requestedPage.endsWith("Page1.jsp")) /* This is the login page */I think I'm missing something that would be obvious to anyone who knows better than me. Any ideas?

    Dear Ginger and Boris,
    thanks for the information - the problem seems to ocur in EP7 as well, Boris told me it is fixed in SP15. We are on SP14 now, so there is hope !
    actually the information in the oss note stated above is also true, as we have an Oracle DB. On a similar demo system (only difference is SQL DB) the hyphen search works !
    best regards, thank you !
    Johannes

  • How do I pass the part of the url that matched url-pattern to JSP?

    Hello,
    I have a web app called ptest.war deployed in JBoss. In my web.xml I have:
    <servlet-mapping>
    <servlet-name>GetImage</servlet-name>
    <url-pattern>/images/*</url-pattern>
    </servlet-mapping>
    and:
    <servlet>
    <servlet-name>GetImage</servlet-name>
    <jsp-file>DoImageRequest.jsp</jsp-file>
    <load-on-startup>1</load-on-startup>
    </servlet>
    In DoImageRequest.jsp, I want to know what was requested by the user (so I can set some response headers and then re-direct to the image they requested), so if I go to:
    http://localhost/ptest/images/i1.jpg
    I want to have "/images/i1.jpg" available to me in the JSP, so that I can do a jsp:forward to the requested image, but none of the getters on the request object give me that string.
    request.getServletPath() returns "/images" and request.getPathInfo() returns "/i5.jpg".
    So do I need to construct what the user asked for by concatenating these two values?
    The reason I was testing this was because I wanted to set no-cache in the response to the request for the image. Is there an easier way of doing that without having to direct the request to a jsp so that the jsp can set the response headers, which seems a little long-winded to me?
    Many Thanks,
    Paul Smith

    I want to have "/images/i1.jpg" available to me in the JSP, so that I can do a jsp:forward to the requested image, but none of the getters on the request >object give me that string.Even if you manage to construct that, such a request path would be intercepted by your jsp because it maps to /images/*.
    You would end up with infinite recursion.
    To circumvent that, you can change the folder that stores the image - call it something like img and then you just need append the request.getPathInfo().
    getServletContext.getRequestDispatcher("/img"+req.getPathInfo()).forward(req,res);
    The reason I was testing this was because I wanted to set no-cache in the response to the request for the image. Is there an easier way of doing that >without having to direct the request to a jsp so that the jsp can set the response headers, which seems a little long-winded to me?You could have a filter intercept all requests for *.img, add the response headers and pass the request along to the actual resource. That would be easier and more intuitive. If your web app has a good fronting like an apache web server, I think you can cache your images on the web server and set a property that would instruct the web server to set a no-cache header in response when it serves the image.
    ram.
    Edited by: Madathil_Prasad on Mar 26, 2010 7:20 PM

  • How to find out if a pattern validation failed

    Hello,
    I have Designed a form with several pattern-validations on it. LC's default behavior on such validation is that a message will be shown, but the user is still allowed to leave the field. Even if the entered data is crap.
    So is it there any way to clear the field after a validation error or enforce the user to go back to the field?
    I know it it possible to mark the validation as an error, so submitting will refuse. But in my case I have a custom submitting procedure, so this won't work.
    Thanks for your help.

    Edgar:
    Instead of using a validation picture, you could put the logic in a FormCalc validation script and use the format() method. Then if it fails you can clear the field and setFocus() back into the field.
    Alternatively, you could use a display picture and your validation script could compare rawValue with formattedValue. If a display picture fails, then rawValue and formattedValue will be the same.
    I've described this in more detail (with a sample) at:
    http://blogs.adobe.com/formfeed/2009/02/calling_formcalc_functions_fro.html
    John Brinkman

Maybe you are looking for