Help with regex. How to escape "|"

Hi, this is my code:
String line = "00001740 00 a 01 able 0 003 = 04866033 n 0000 = 05262099 n 0000 ! 00002062 a 0101 | (usually followed by `to') having the necessary means or skill or know-how or authority to do something; "able to swim"; "she was able to program her computer"; "we were at last able to buy a car"; "able to get a grant for the project"  ";
//I want to capture with group(0) only the first half og the line, that preceeding "|".
//Could anyone help me figure out why this piece of code does not work ?
String patternStr = "(.*?)|(.*?)";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(line);
    boolean matchFound = matcher.find();
    if (matchFound){
         relLine  = (String)matcher.group(0);
         System.out.println("LINE " + relLine);
         } else {
        System.out.println("ATTENTION MATCH WAS NOT FOUND FOR LINE " + line);
              }

"\\|"You need to use the backslash.
However, since backslash is also a special character in a Java String literal, you need two of them. The first one gets eaten by the compiler, escaping the second one, which is used by regex to escape the pipe.

Similar Messages

  • Help with REGEX to block invalid characters

    I have a regex that is used to block unusual characters from being entered into a user name, so they can put pipes etc in there, I just want 0-9 and a-z (upper or lowercase), but I just noticed that it's not working. I am not up to speed on regex, I took this from somewhere else
    here is the expression:
    <cfif len(trim(ReReplaceNocase(form_username, '^[A-Za-z][A-Za-z0-9_]*', '', 'ALL'))) gt 0>
    It is failing when I enter 2kljlkll3456 as the username
    Anybody have any idea why it's not working?
    After posing this I found out that the issue is that it does not allo me to have a username that starts with a number, only a letter, anybody have any idea how to fix that?
    Thanks
    Mark

    Hey Dan,
    I found a link that explained how the regex is actually formed which helped!
    http://stackoverflow.com/questions/336210/regular-expression-for-alphanumeric-and-undersco res
    Now I have managed to get a basic understanding of how they are formed the fix was easy
    I had:
    <cfif len(trim(ReReplaceNocase(form_username, '^[A-Za-z][A-Za-z0-9_]*', '', 'ALL'))) gt 0>
    But should have had
    <cfif len(trim(ReReplaceNocase(form_username, '^[A-Za-z0-9_]*', '', 'ALL'))) gt 0>
    Simply removing [A-Za-z] from the start fixed it. I get it now ... so the first section defined the first character which was restricted to A-Za-z only.
    I'll mark this as answered
    Thanks
    Mark

  • Further help with regex

    Hi,
    I want to detect the presence of "fromCharCode" in a String.
    But only when it is not preceded by "String." and not followed by "(34)".
    I have already managed to do the "String." with this regex: (?<!string\.)fromCharCode --> uses the negative look behind
    This will detect the "fromCharCode", but not when it is preceded by "String.".
    But I don't know how to do the "(34)" ?
    Can somebody help me with this?

    thanks, but the problem with this regex now is that "fromCharCode" is not detected in the String fromCharCode(34) and the String String.fromCharCode.
    The text "fromCharCode" must only be detected when not preceded by "String." AND not followed by "(34)".
    Can you do this?

  • Need a Little Help with Understanding How Layers Work in PSE

    I have PSE version 5 and I am using it on a PC with Windows XP.
    As I get more into editing photos, I am enjoying it, but confusion is also starting to set in.  My question is about layers.  When I go to edit a photo for web/email use only, I start by resizing it to 72 ppi and then reducing the pixel dimensions quite a bit.  Then after that I normally go through this process of editing the photos by using normally about three different commands.  One is Levels, then I may go to Shadows/Highlights, and then my last command is always sharpening.  I used to do this all on one layer, but then finally learned to put each one of these editing features on their own layer so I can make changes/deletions without too much trouble.
    When I started doing the separate layers, I was just making a copy of the background layer each time and then touching that particulay layer up with Levels, Sharpen or Shadows/Highlights.  But I noticed that depending on the order in which the layers were placed, some changes would be obscured.  If I put the Levels layer on top, and then the Sharpen layer and the Shadows/Highlight layer below that, the sharpen effect and the shadows/highlights effects were no longer there.  I could only see the levels effect.  The photo was still not sharp and so on.  If I put the Sharpen level on top, then the photo showed the sharpen effects, but now the Levels and Shadows/Highlights effects were no longer visible.
    So then I started to make a copy of the background initially, do a levels adjustment here, then, instead of making a copy of the original background layer again, I would make a copy of the layer that now has the levels changes on it.  Then do the sharpen effect on this layer.  This way I had all the changes on one layer as long as I put that layer as the top layer.  But then I realized that I once again fI didn't have a layer with only one fix on it.   Finally, I started to use an adjustment layer for Levels, put this as the top layer, make a copy of the background layer, do shadows/highlights, copy background layer again, do sharpen, and this seems to work a little better.  But even with this way, depending on what order I place the sharpen and shadows/Highlights layers, one of them still seems to get obscured by the layer above it.
    Can someone help me to understand how layers work in this regard.  I am obviously a little confused here.
    Thanks,
    Lee

    You really aren't working efficiently. First of all, do your editing on a full sized version of the photo, since you have more data for PSE to work on and will get better results. Use save for web to make your web/email copy when you are totally done. You can resize it down on the right side of the save for web window.
    Duplicating a regular layer makes an opaque layer that obscures the layers below it. It's best to start off by not working on your original image, so that you don't have to worry if you mess up. If you're working on a copy, you can work right on the background layer, or only duplicate the layer once and apply your changes to that. So you'd apply both shadows/highlights and sharpening to the same layer.
    Finally, you should use an adjustment layer for levels. Layer>New Adjustment Layer>Levels. This will put levels on a transparent layer above your image, but you will need to click the layer below it to get back to a pixel layer to apply other changes (you can't sharpen an adjustment layer, for example).

  • Need help with Regex

    Still trying to solve a problem in Dreamweaver - and think that using Regex is the way to go - but never used it before
    I have 2 tables on 4000 pages and want to delete 1 table from each page but they have different links and content etc
    The 2 tables on each page  are also distinguished by different table widths
    If the table I want to delete looks like this example below - what Regex do I use to make the find and replace delete only the table with <table width=100%>
    <table width="100%"><tr><td><a href="aberley.htm"><font size="-7">aberley</font></a></td></tr></table>
    Many thanks

    Thanks Nancy O - you are a Star - have tried it out and it worked perfectly
    - and this will new piece of knowledge will save me many many hours of work
    I had no previous knowledge of that Find and Replace Special Tags option
    Thanks again for your help - it is greatly appreciated

  • With regex, how can I combine backreference and repetition

    Let's say I have a four byte long input string, consisting of four hex values:  0x03 0x03 0x03 0x03
    If I use repetition in a regex string pattern: .{4}  it matches true.
    Also, if I use backreference in a new regex pattern: (.)\1\1\1  it matches true.
    But I want to combine the two.  Say that the first byte is a length byte of the following bytes.  Then I'd believe it should be possible to do this: (.).{\1}
    The thought is that the first byte is stored as "variable" \1, and reused as the repetition counter.  So in this case, all four byte strings starting with 0x03 should match true.
    But this doesn't work!
    What am I doing wrong here?  Is this just not possible?

    ojohnsen wrote:
    No RegEx experts here?
    I am not an expert, but I play one on this forum.  To show something is possible is quick, since you can demonstrate it.  To show something is impossible is a bit harder.  For now I say, not possible.  And you do realize that .{4} will match any four character string, not anything repeated four times, right?
    That said, it sounds like you know the format of your string, so perhaps a regex would not be your first choice.  You could split a string, read the first byte, then construct a regex like ^(.)\1{length-1} and see if the remainder of the string begins with a value repeated length times.

  • Help with knowing how to save and archive FCE edits properly

    Hi there
    When I capture footage from my camcorder, the original video files seem to get saved into my 'Capture Scratch' folder within the 'Final Cut Express Documents' folder. I have now edited the movie and want to save the finished edit, along with the timeline on my external harddrive.
    My question is, what Final Cut Express files do I need to transfer over to my external hard drive so that I have all the necessary files for my edit to work perfectly, should I wish to continue editing it in the future? Do I have to keep transferring the whole 'Final Cut Express Documents' folder for each and every new video I make?
    When I say that I want to save the finished edit, i mean that I want to be able to have the exact FCE page come up just as it did when I was editing it (ie. with original video files in the bin, timeline, transitions etc).
    In Imovie 6HD it was easy as all the workings, the actual edit and the original footage were saved within one folder, but there seems to be various different parts to my Final Cut Express edit dotted around the folders.
    Can anyone help me?
    Many thanks
    Daniel

    Hi there Tom
    Thank you so much for getting back to me so quickly.
    Ok, so I transfer/copy the video contents of the 'Capture Scratch' project, but where is all the timeline info, transition info etc (ie. the processes that I went through to edit the actual video footage) to accompany these original video files? Do I need to transfer this file along with the 'Capture Scratch' files to have all that I need for future re-editing?
    One last question. If I have a finished edit in HD and dont want to have to store all the footage that I didnt need, can I save the finished edit as a 'Self-Contained Movie'? If I do this will the HD footage be compressed in some way or will its quality stay as original? Can it be imported back into FCE at a later date for re-editing?
    Im so sorry for all these questions
    Daniel

  • HT4623 help with iOS7 how do you kill the programs that you visit on your phone to save the battery life?

    how to kill programs with IOS7 that you visit on your iphone to save the battery?

    Double tap Home button, swipe the app UP to delete from multitask-list.

  • Please help with regex

    Hi
    I want regex for following condition:(case insensitive)
    1)First letter must be always an alphabet(case insensitive).
    2)It should be maximum of 64 characters.
    3)Rest all letter can be number alphabets and underscore.(case insensitive)
    4)Defines an inverse domain name like "com.sun.yyyy.xxxx". Restricted to max 20 dot-separated identifiers of max. 30 chars each.
    5)After . there must not be number for eg com.sun.1awt is not valid.

    Not properly tested, but here goes:
    // some tests
    String[] tests = {
      "com.sun.yyyy.xxxx",
      "com.sunsd.yy435yy.xxxx",
      "com.sun.yyyy.5xxxx",
      "com.sun.yyyeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey.xxxx",
      "com.sun.yyyy..xxxx"
    // An atom must start with a letter follwed by 0 to 29 numbers, letters or underscores
    String atom = "[a-z][a-z0-9_]{0,29}";
    String regex =
      "(?i)"+                  // ignore case
      "(?=^.{1,64}$)"+         // must be between 1 and 64 characters
      atom+                    // start atom
      "(?:\\."+atom+"){0,19}"; // followed by 0 to 19 atoms with a '.' in front of it
    for(String t: tests) {
      System.out.println(t.matches(regex)+" -> "+t);
    }

  • Help with regex

    Hi,
    I have an expression like this
    <select name="contact_list">
    <option value="SS109445168429566">Mark
    <option value="SS109445173826096">Keith
    </select>
    this is not in its entire state, i have trimmed the expression to make it a bit readable
    no what i want is both the options value to be extracted and stored in a value i am using the pattern like this to do the same
    pattern="(?s)(?i)<option value="(.*?)""
    but i am always getting th first value only, whereas i want both the values.
    Can anyone help me on this.
    Thank you in advance

    How are you using the regex?  Pattern p = Pattern.compile("(?is)<option value=\"(.*?)\"");
      Matcher m = p.matcher(input);
      while (m.find())
        System.out.println("value is " + m.group(1));
      }That should work.

  • Help with regEx or any other idea ....

    my program reads from the keyboard... if there is a character like K the description is printed ... but if there is also a number between 2.....10 and K then the number should be printed too
    example :
    reading from keyboard: 4K
    then 4 + description of K should be printed
    I tried this:
    else if(n.equals("S") && notationShort.containsKey("S")|| (n.equals.{2,10})
         {System.out.println(notationShort.get("S"));}but its not working ... can someone help please?
    thank you in advance!

    sabre150 wrote:
    new_in_JAVA_but_soon_not wrote:
    not sure if someone understand my question ?? or no one know how to solve it?
    hmmmHmmm - I certainly don't fully understand what you are trying to do. It seems to me you want to test for some digits followed by one of a set of letters. If this is the case then what set of letters and how many digits. Also, can other things come before the digits or follow the letter?
    You need to think a bit more about your questions if you expect any answer other than a wild guess.ok how can i write in regular expression the range between 2..10 ?

  • I need help with Regex, please !

    Hello,
    I would like to build the following expression: I want matcher a succession of words (e.g. separated by 1 space), but not containing a specifc word.
    Let's me give you an example to be more explicit:
    String regex = "([\\S]+ )+"; that exprisses a succession of words, but I want to supplement my regular expression (regex) to indicate that I don't want the word "MANU" for example.
    string input = "TUTU TATA TITI MANU TOTO TITI TATA MANU"
    I would like the following results:
    "TUTU TATA TITI MANU"
    "TOTO TITI TATA MANU"
    but not these one:
    "TUTU TATA TITI MANU TOTO TITI TATA MANU"
    Thank you for your answers

    Right, I forgot that the matcher would bump along and try "(?!MANU\\b)\\S+" at the second letter of each group. The solution is to use \w instead of \S, and anchor the match at the beginning of the word with \b:import java.util.regex.*;
    public class Test
      public static void main(String[] args)
        String target = "TUTU TATA TITI MANU TOTO TITI TATA MANU";
        Pattern p = Pattern.compile("(?!MANU\\b)\\b\\w+(?: (?!MANU\\b)\\b\\w+)*");
        Matcher m = p.matcher(target);
        while (m.find())
          System.out.println(m.group());
    }

  • Need help with Regex replace in DW

    I'm converting a document to HTML that came with mixed case image filenames.  Our standards say that all filenames must be lowercase, so I used a CF script to convert them, but the src links in the HTML doc tag still shows the uppercase name.
    I planned to do something like this:
    Find:
    <img src="(.+?)" alt="(.+?)" />
    Replace:
    <img src="$1" alt="$2" />
    but I can't seem to get it to lowercase the $1 argument.  I've tried \L and all the normal things, but I just get the literal replacement.  Any tips?

    Use Regular Expressions
    "Big_Slick" <[email protected]> wrote in
    message
    news:f0ip6t$ee8$[email protected]..
    >I need to know how to Find/Replace for more than one tag
    at a time
    >(I.E--Search
    > for <table>, <tr>, and <td> or
    and and remove the code) As it is
    > right
    > now, it will only search for one tag at a time but I
    would assume DW can
    > handle
    > this kind of thing.
    >
    > Thanks in advance.
    >

  • Pls help with RegEx

    Hello,
    I can't figure out how to write a simple regex.
    For example I have a String ("12345678") and I'd like to find out whether my String contaings "456".String str1 = "12345678";
    String str2 = "456";
    System.out.println(str1.matches(?)); // what is the right regex to find out if str1 contains str2?

    For such a simple example, regex is overkill. You could just test for String.indexOf("456") >= 0
    However, to answer your question, the expression would be:
    .*456.*

  • Help with regex pattern matching.

    Hi everyone
    I am trying to write a regex that will extract each of the links from a piece of HTML code. The sample piece of HTML is as follows:
    <td class="content" valign="top">
         <!-- BODY CONTENT -->
    <script language="JavaScript"
    src="http://chat.livechatinc.net/licence/1023687/script.cgi?lang=en&groups=0"></script>
    <a href="makeReservation.html">Making a reservation</a><br/>
    <a href="changeAccount.html">Changing my account</a><br/>
    <a href="viewBooking.html">Viewing my bookings</a><br/>I am interested in extracting each link and the corrresponding text for that link into groups.
    So far I have the following regex <td class="content" valign="top">.*?<a href="(.*?)">(.*?)</a><br>However this regex only matches the first line in the block of links, but I need to match each line in the block of links.
    Any ideas? Any suggestions are appeciated as always.
    Thanks.

    Hi sabre,
    thanks for the reply.
    I am already using a while loop with matcher.find(), but it still only returns the first link based on my regex.
    the code is as follows.
    private static final Pattern MENU_ITEM_PATTERN = compilePattern("<td class=\"content\" valign=\"top\">.*?<a href=\"(.*?)\">(.*?)</a><br>");
    private LinkedHashMap<String,String> findHelpLinks(String body) {
        LinkedHashMap<String, String> helpLinks = new LinkedHashMap<String,String>();
        String link;
        String linkText;
          Matcher matcher = MENU_ITEM_PATTERN.matcher(body);
          while(matcher.find()){
            link = matcher.group(1);
            linkText = matcher.group(2);
            if(link != null && linkText != null){
              helpLinks.put(link,linkText);
        return helpLinks;
    private static Pattern compilePattern(String pattern) {
        return Pattern.compile(pattern, Pattern.DOTALL + Pattern.MULTILINE
            + Pattern.CASE_INSENSITIVE);
      }Any ideas?

Maybe you are looking for