Question related to Regular Expressions

Hello,
I have the following Java program where i would like to accept words which contain the following characters
[A-Za-z ./-]*
Letters A-Z a-z
Other characters . / - space
public static void main(String args[]) {
      String regex = "[A-Za-z _./-]*";
      String input = "Hello-how--are you--";
    boolean isMatch = Pattern.matches(regex, input);
    System.out.println(isMatch);//return true
  } The previous code accepts all the apperances of dashes, but i wouldn't like to accept dashes which are next to each other.
I would like to accept words like ab-cd-ef-gh but i wouldn't like the dashes to be -- like this, next to each other i would like to have at least one character between them.
Accepted How-are-you-today
Not accepted hi- or az--z
What am i supposed to do in order to have the desirable result?
Thanks, in advance!

DarrylBurke wrote:
sabre150 wrote:
All regex are obvious and no documentation is required. That is the philosophy I used for many years so as to keep me permanently in work.And one fine day we'll see a forum post:
Can someone help explain this regex to me? The developer who wrote it has since retired.
edit Couldn't post the regex due to the forum's limit of 7500 characters :(;-)Yep - I will then suggest that they supplement my pension and pay me an exorbitant amount to fix their problem.

Similar Messages

  • Question about match regular expression

    Colleagues,
    Very stupid question. I would like to get substring between "..." symbols. For example, string 02 July from Explosion occurred on "02 July", 2008.
    How to do this with single Match Regular Expression?
    For example such expression ".*" will give me "02 July":
    But I would like to get it without " symbols!
    I tried this "[~"]*" and this "[~"].*", then read this and this , and all without success... But I'm sure it should be possible. Can you help me?
    Andrey.
    PS
    This regular expression should give exactly the same output as following construction:

    I'm only using 7.0 now, but you can do this with Scan from String...
    %[^"]"%[^"]"%[^"]
    Message Edited by Phillip Brooks on 07-02-2008 02:47 PM
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness
    Attachments:
    NotPCRE.png ‏20 KB

  • Question sql and regular expressions

    I am trying to write a query where a like with a regular % wont work.
    select <cols>
    from <tab>
    where col2 starts with 'SOME CHARACTERS' The field can have 0 or more empty spaces before there are more characters. However, I know what those characters are. How do I do 0 or more characters?
    select mycols
    from myemptable
    where empname like 'JO%JO'There could be 0 or more empty spaces between JO and JO, but the next character is 'JO' . This is a general case. So it could be something else.
    so % won't work. since i could get back 'JO HA JO' which I dont want.

    Regular expressions where introduced with Unix in 1973.
    It shouldn't be difficult to find online resources.
    You seem to belong to the big class of users in this forum who doesn't know how to use the Internet, or find that to demanding.
    The solution can of course be found in 5 minutes, of 2 to start my database especially for you at almost midnight.
    with a as (select 'JO JO' col1 from dual
                  union
                   select 'JO    JO' col1 from dual
                   union
                  select 'JO HA JO' col1 from dual
                  union all
                  select 'JO  JO BLEH' from dual)
       select * from a where regexp_like(col1,'^JO( +)JO.*$')
    You find out what it means as exercise.
    Sybrand Bakker
    Senior Oracle DBA

  • A question about using regular expression

    Hi,
    This is a part of HTML file.
    <SPAN>how</span>
    <SPAN>are</span>
    <SPAN>you</span>
    I want to search string between each pair of <SPAN> , </SPAN> tags by using Regular Expression.
    For example:
    how
    are
    you
    If I use following method
    String regx="<SPAN>(.+)<SPAN>";
    Matcher m=Pattern.compile(regx).matcher(str);
    int currentLoc=0;
    while(currentLoc<str.length()){
        if(m.find(currentLoc))
        System.out.println(m.group(1));
        currentLoc=m.end();
    }The content between first <SPAN> and last </SPAN> will be searched.
    How to solve this problem?

    Use a non-greedy match:
    (?s)<SPAN>(.+?)<SPAN>(?s) makes the dot match the line terminator (same as setting the dot all option)

  • Question about creating regular expression

    Hi!
    I am creating parser for the specific configuration file. Now I have got small problem: I need to find the line with text in format "key value".
    For example I have this text file:
    entry {
         key value
         key2 value2
         entry2 {
         }I wish to find, using java.regex, these two lines: "key value" and "key2 value2". But how?

    Is your problem that you don't understand regular expressions? If so then this might help. Or is your problem you don't understand Java regular expressions? If so then this might help. Or is your problem that you don't understand how to read lines of a file? If so then this might help.

  • Related to Regular Expression

    The below code is removing the comma character from the expression.
    public static String formatString() {
    String s="��������abcdefghijklmnopqrstuv,,,,,,,,,!``~!@#$%^&*()--+={}[]|\'':;?/><.,wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()----+=:;?.,1234567890";
    String temp = Normalizer.normalize(s, Normalizer.DECOMP, 0);
    String st=temp.replaceAll("[^\\p{ASCII}]","");
    String string2=st.replaceAll("[\\x01-\\x08,\\x0b-\\x0c,\\x0e-\\x1f,\\x7f-\\xffff]", "");
    return string2;
    If I am deleting comma and writing expreesion as below:
    String string2=st.replaceAll("[\\x01-\\x08\\x0b-\\x0c\\x0e-\\x1f\\x7f-\\xffff]", "");
    Then it is adding the comma character.
    But I want to add comma with the existing expression only..
    Is there anyway that we can include comma within expression without removing it from expression?
    Please suggest me.

    Rocky.S wrote:
    Ok..I am using a piece of code for removing all diacritics characters as well as some other characters also.The below expression:
    String string2=st.replaceAll("[\\x01-\\x08,\\x0b-\\x0c,\\x0e-\\x1f,\\x7f-\\xffff]", "");
    Working fine but with the replacement of comma character only which I required for my application.Could you plese let me know how I will add comma character with this expression????I suspect I am wasting my time since I can't really see what you are asking but here goes - the regex "[\\x01-\\x08,\\x0b-\\x0c,\\x0e-\\x1f,\\x7f-\\xffff]" already has the ',' character added 3 times since ',' is not used to separate ranges.
    Are you asking how to remove JUST comma characters? If so then it is as simple as String string2=st.replaceAll(",", "");

  • Basic question on this Regular Expression syntax

    The below query returns the string before the first comma.
    SQL> select regexp_substr('JACK,AND,JILL,WENT', '[^,]+' ) FROM DUAL;
    REGE
    JACKWhat does
    [^,]do? Does it just mean, look for comma (,)
    What does the plus sign in
    '[^,]+'do?
    These are not mentioned in the 10GR2 's SQL Reference for REGEXP_SUBSTR function, hence i am posting this in OTN.

    Hi, Hoek,
    hoek wrote:
    Hi Pete,
    They are documented:
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm#sthref547
    Metacharacter     Description
    ^     Anchor the expression to the start of a lineIt looks like you were in a hurry.
    The ^ in
    [^,]+is the non-matching character marker: "Matches any single character not in the list within the brackets"

  • Regular Expressions find and replace

    Hi ,
    I have a question on using Regular Expressions in Java(java.util.regex).
    Problem Description:
    I have a string (say for example strHTML) which contains the whole HTML code of a webpage. I want to be able to search for all the image source tags and check whether they are absolute urls to the image source(for eg. <img src="www.google.com/images/logo.gif" >) or relative(for eg. <img src="../images/logo.gif" >).
    If they are realtive urls to the image path, then I wish to replace them with their absolute urls throughout the webpage(in this case inside string strHTML).
    I have to do it inside a servlet and hence have to use java.
    I tried . This is the code. It doesn't match and replace and goes inside an infinite loop i.e probably the pattern matches everything.
    //Change all images to actual http addresses FOR example change src="../images/logo.gif" to src="http://www.google.com/../images/logo.gif"
              String ddurl="http://www.google.com/";
    String strHTML=" < img src=\"../images/logo.gif\" alt=\"Google logo\">";
    Pattern p = Pattern.compile ("(?i)src[\\s]*=[\\s]*[\"\']([./]*.*)[\"\']");
    Matcher m = p.matcher (strHTML);
    while(m.find())
    m.replaceAll(ddurl+m.group(1));
    what is wrong in this?
    Thanks,
    Rajiv

    Right, here's the full monte (whatever that means):import java.util.regex.*;
    public class Test1
      public static void main(String[] args)
        String domain = "http://www.google.com/";
        String strHTML =
          " < img src=\"images/logo.gif\" alt=\"Google logo\">\n" +
          " <img alt=\"Google logo\" src=images/logo.gif >\n" +
          " <IMG SRC=\"/images/logo.gif\" alt=\"Google logo\">\n" +
          " <img alt=\"Google logo\" src=../images/logo.gif>\n" +
          " <img src=http://www.yahoo.com/images/logo.gif alt=\"Yahoo logo\">";
        String regex =
          "(<\\s*img.+?src\\s*=\\s*)   # Capture preliminaries in $1.  \n" +
          "(?:                         # First look for URL in quotes. \n" +
          "   ([\"\'])                 #   Capture open quote in $2.   \n" +
          "   (?!http:)                #   If it isn't absolute...     \n" +
          "   /?(.+?)                  #    ...capture URL in $3       \n" +
          "   \\2                      #   Match the closing quote     \n" +
          " |                          # Look for non-quoted URL.      \n" +
          "   (?!http:)                #   If it isn't absolute...     \n" +
          "   /?([^\\s>]+)             #    ...capture URL in $4       \n" +
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
        Matcher m = p.matcher(strHTML);
        StringBuffer sbuf = new StringBuffer();
        while (m.find())
          String relURL = m.group(3) != null ? m.group(3) : m.group(4);
          m.appendReplacement(sbuf, "$1\"" + domain + relURL + "\"");
        m.appendTail(sbuf);
        System.out.println(sbuf.toString());
    }First off, observe that I'm using free-spacing (or "COMMENTS") mode to make the regex easier to read--all the whitespace and comments will be ignored by the Pattern compiler. I also used the CASE_INSENSITIVE flag instead of an embedded (?i), just to remove some clutter. By the way, your second (?i) was redundant; the first one would remain in effect until "turned off" with a (?-i). Another way to localize a flag's effect by using it within a non-capturing group, e.g., (?i:img).
    As jaylogan said, the best way to filter out absolute URL's is by using a negative lookahead, and that's what I've done here. The problem of optional quotes I addressed by trying to match first with quotes, then without. The all-in-one approach might work with URL's, since they can't (AFAIK) contain whitespace anyway, but the alternation method can be used to match any attribute/value pair. It's also, I feel, easier to understand and maintain. Unfortunately, it also means that you can't use replaceAll(), since you have to determine which alternative matched before doing the replacement, but the long version is still pretty simple (especially when you can just copy it from the javadoc for the appendReplacement() method, as I did).

  • Match Regular Expression not returning submatches.

    I am having an issue with Match Regular Expression not returning the appropriate number of submatches.  My string is as follows:
    DATAM    995000    1.75    0.007    -67.47    24.493    99.072
    The spaces are tabs and the length is not fixed so simple string manipulation is out of the question.  The regular expression I was trying is as follows:
    (?<=\t)[0-9\.\-]*(?=(\t|$))
    It successfully returns Whole Match as the first number but no submatches are returned.  I've tried simpler expressions which work in Matlab and EditPad Pro such as [0-9.-]* but never got the same answer in Labview.
    What is going on?

    Here is an image of the VI.  The top portion is the input from our Licor 7000.  The bottom portion is the bit of code I'm having problems with.
    Attachments:
    matchregularexpression.jpg ‏336 KB

  • Archived Forums N-R Regular Expressions

    Hi,
    I am trying to post a question in the Regular Expression forum; however, I am unable to select the appropriate forum category as I don't see it listed.  Can someone tell me how to post a question in the Regular Expressions forum?
    Thanks

    That forum was retired and is now read-only. According to the announcement;
    Any future posts on this topic should be put in the 
    .NET Framework Class Libraries forum.
    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows]
    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights.

  • Regular Expressions - Questions to SME

    Ralph Benzinger presented an online meetup on the topic of Regular Expressions.  The presentation (slides only) can be found <a href="http://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/866072ca-0b01-0010-54b1-9c02a45ba8aa">here</a>
    Unfortunately the recording is not going to be available, but Ralph has been generous enough to agree to answer questions posted to this sticky thread.
    cheers,
    Marilyn

    Hello Peter,
    You're welcome!
    Alas, I was unable to locate the regex documentation on help.sap.com either.  In fact, I'm not even sure it has already been updated for 2004s.  I recommend that you use the online documentation within the system, e.g., from transactions SE38 or SE80.  Do an index search for "regex", and you'll be directed to REGEX, FIND and REGEX, REPLACE, both of which have extensive subsections on regexes.
    The class cx_sy_regex is an exception class that is thrown by FIND, REPLACE and cl_abap_regex in case of an invalid regex, such as ".\1" (there is no capture group that back reference \1 can refer to).  If the pattern is known statically, the syntax check will report this error, but for statements like "FIND REGEX pat IN text.", the actual pattern is only known at runtime.
    The cx_sy_matcher class (and its subclasses) similarly indicate some invalid states, for example trying to call "cl_abap_matcher->replace_found( )" when the matcher has no current match to replace (e.g., replace_found( ) called twice in a row).
    Please let me know if I can provide some additional information.
    Regards
    Ralph

  • Jakarta regular expression package question

    hi everyone,
    I'm trying to figure out how to use the jakarta regular expression package to return an array of matches ?
    For example let's say i had StringBuffer object consisting of say 30,000 characters and I needed to return all the matches that started with < and ended with > (like all the xml tags )
    anybody know how I could use the package to return an array of matches ?
    thanks in advance
    stev

    okay -- perhaps i should maybe rephrase my question for this package -- perhaps it is more popular ?
    anyone know how i can pass a regular expression to it and retrieve a String array
    In my specific situation , I have a StringBuffer with about 10,000 characters in it ?
    any ideas how i would use regular expressions to look through this Stringbuffer object
    thanks
    stephen

  • Regular expression usage question

    Hi there.
    I have a 200 bytes EBCDIC variable record which I need to break down into fields. Fields are positional and are either text, binary numbers, packed-decimal and 64bytes long numbers.
    My question is. Can regular expression handle this complex data.
    I want to isolate each field into their corresponding format. EBCDIC into ASCII text, binary into java Integer and so on.
    The reason for using reqular expression is because the record format could change and regular expression would be easier to modify without having to change the code.
    Your words of advice are highly appreciated.
    Please advice.
    Regards,
    Ulises

    Regular expressions? I don't think so.
    If you have a situation where positions 1-3 might be a binary number like client number, and the format might change so it moves to positions 12-14, then you could certainly write a record-format class to encapsulate that sort of information. In fact that would be a very good idea. But I can't imagine how a regular expression would help in getting a number out of three bytes, for example.

  • Help: Regular Expression question??

    Hello,
    How can I extract the following content using Java Regular expression?
    <tr bgcolor="#333333">
         <td class="title" colspan="4" height="18"> <b>SUPER_1</b> - SUPER_2</td>
    </tr>
    <tr bgcolor="#333333">
         <td class="match-light" width="45" height="18"> </td>
         <td class="match-light" colspan="3" width="286" align="right">March 19 </td>
    </tr>
    <tr>
         <td colspan="4" height="1"></td>
    </tr>
    <tr bgcolor="#cfcfcf">
         <td width="45" height="18"> FT</td>
         <td width="118" align="right">SUPER_3</td>
         <td width="50" align="center"><a class="scorelink" target="details" onclick="showDetails();">999 - 888</a></td>
         <td width="118">SUPER_4</td>
    </tr>From the above contents, How can I define a regular expression for extract the "*SUPER_1*", "*SUPER_2*", "*March 19*", "*SUPER_3*", "*999*", "*888*" and "*SUPER_4*" ????
    Please help.
    Best regards,
    Eric

    Kayaman wrote:
    Why not use a better way than regex, like an actual HTML parser (or XML if you have it well-formed)? People seem to love parsing (or rather, asking help how to parse) HTML with regex for some unknown reason.Indeed.
    Read this (hilarious):
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

  • Regular Expression Question

    Hi all,
    I am suffering in java regular expression, and I hope you guys can help me out. I want to use the String api ".matches" to find out any string pattern like "xxxx.xxxx" where xxx can be only english word(both upper and lower case). Actually I will use this kind of expression to represent the cross join SQL statement in my java class, like "tableA.name = tableB.name", where they should be english letter only. I tried to use MyString.matches("^[A-Z] + \\. + ^[A-Z]") in my java program, but seem it doesn't work. Can you guys figure out the right expression for me ?? Many thanks
    Transistor

    Thanks for your prompt response, I tried your code, however, it doesn't work out.
    I put your code like the following:
    if ( searchCriteria.getStringPair().getValue().trim().matches("[A-Za-z]+\\.[A-Za-z]+") {...some action }.
    Seems the java program never reach this expression.
    Kindly remind that I wan to expression anything like "xxxxx.xxxxx" where xxxx can be a word.
    Myriads of thanks
    Transistor

Maybe you are looking for

  • How to upload excel file in Webdynpro application using ABAP

    Hi Experts, Am developing a webdynpro application in which it will take an excel file as input and display the contents in the form of a table in output. I am able to upload tab delimited text file and populate the table using the below code but not

  • Authentication error (0:5:111) error during Online Oracle DB backup

    Hi All, larger database backup have been failing with below error from Networker backup tool. channel CH5: starting piece 1 at 06-JUN-12 RMAN-03009: failure of backup command on CH4 channel at 06/07/2012 05:24:16 ORA-27192: skgfcls: sbtclose2 returne

  • IPhoto books archival???

    Color photographs I took 20 years ago are now fading and turning yellow in my albums while the black and white pictures in my grandmother's scrapbooks look just fine. Does anyone know how long iPhoto books last? Will my kids be able to show them to t

  • Purchased a movie, no longer on iTunes?

    Bought the latest KING KONG extended version (with Jack Black) for my kids.  Lost the copy I had in my iTunes library and I cannot find it in the iTunes Store now.  What's up?  I'm afraid to buy movies now not knowing whether they'll disappear or not

  • Activity Type Mandatory in Repetitive Confirmation

    Dear guru , When I run confirmation (MFBF/MF42N) i can post actual activities for a specific operation. I have one of this activity not relevant for controlling , itu2019s only used as statistical object for PP. Why the system consider the activity t