Regex on a String?

I have an array of strings. Each string is pretty big. I just want to match it if anywhere in the string there is a word.
For example 'test'.
In perl, it would be something like this:
if ($myString =~ /test/) { print ("Found it\n"); }That works. In java, not so much. I'ved tried 'test', '.*?test.*?' and about a million other combination.
This is my code:
     boolean isMatch = Pattern.matches("test", myString);
     if (isMatch) { System.out.println("Found it\n"); }

1.) Using a regex for simply finding some plain text is overkill and prone to problems (what if your simple text contains ".*"?)
2.) .matches() requires the entire String to match on the regex, you want to use Matcher.find() instead.
3.) myString.contains("test") would have the exact same result and be a lot easier.
Edit: if (Pattern.matches(".*test.*"))
} should have worked (the "?" are unneeded, as the "*" already includes the possibility of zero characters).

Similar Messages

  • Write REGEX for a string pattern

    Hi All,
    I am new to using regular expression and writing the patterns, I am looking to write a pattern for below formats and do a FIND REGEX:
    A123456-01-123456789-123   and A123456-01-123456789-ABC
    DATA: regex TYPE REF TO cl_abap_regex,
          res   TYPE        match_result_tab,
          text  TYPE        string.
    CREATE OBJECT regex
        EXPORTING pattern      = '\(.\)\1'
                  simple_regex = 'X'
    FIND ALL OCCURRENCES OF REGEX regex IN text RESULTS res.
    could you help me write the pattern to check if user has entered string in formats A123456-01-123456789-123   and A123456-01-123456789-ABC
    Thank you
    Depp

    Hi Manish,
    First of all sorry for getting back so late..was stuck with project delivery...and Thank you for your solution. Only one scenario failed i.e., I didn't mention that the value 01 in the examples, is constant in strings A123456-01-123456789-123   and A123456-01-123456789-ABC.
    which means it has to be always 01 any other value than 01 the sy-subrc should not be zero.
    I will also be thankful if you could share me the document(s) which help us write the above patterns( '^\w\d{6}-\d{2}-\d{9}-\d{3}$' ; '^\w\d{6}-\d{2}-\d{9}-\w{3}$' ).
    In the meanwhile I will also do some R&D with your reply to solve the failed scenario.
    Regards,
    Depp

  • How to remove all regex in a string?

    hi guys,
    for instance, if you have a string like below :
    String s = "abcd!@3456*&^%()-+";
    how can I remove all the regex and left and numeric and alphabet only?
    I don't really know how to use the java.util.regex !
    Thanks,
    Mark
    Sdalk

    Hmm it was only example how you can filter it
    If you say that "replaceAll(...) is far, far simpler " then please can you write the code how it will look like?
    prometheuzz wrote:
    Juraj wrote:
    Im not sure what you dont like on that code.Here are a couple of things I don't like about it:
    - you didn't follow normal [Code Conventions|http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html] making your code hard to read;
    - it's overly complicated for such a simple task. By complicated I mean it is not clear what that code is supposed to do when reading through it;
    - you stuffed the majority of that code in your main method instead of creating a separate method;
    - your algorithm loops through the AllowedChars array too much;
    - you're concatenating Strings instead of using some sort of buffer.
    At least it works right. It is the simpliest way how to filter StringsNo, replaceAll(...) is far, far simpler.

  • XML Schemas: Regex for empty string?

    Hi Folks,
    It appears that the new Oracle schema processor has a bug in processing regular expressions.
    Example. Create a regular expression which allows a string of characters 5-10 characters in length, or empty.
    Here's the regex:
    [a-zA-Z]{5-10}|()
    When I use this in a pattern facet and in my instance document create an empty element for this type it gives me an error message (sorry, I don't recall the exact message).
    /Roger

    Roger, empty expr is illegal in current implementation. We will add this support in next release.
    Thanks,
    Oracle XML develop team

  • Using Regex to search strings......

    Hey guys,
    I'm trying to scan an html file for specific strings. Basically, I just store the entire html file as a one big long string and then use a regex to scan the string for specific keywords that I am looking for. However, I am not all that familiar with regular expressions in java, and, although I have spent a considerable amount of time reading over the tutorials, I can't seem to get my regular expression to produce the results that I want.
    +More specifically, what I am trying to scan the html string for is a keyword preceded by a non-alphanumeric character([/W] and followed by a non-alphanumeric character. Also, since the keywords generally represent country names, state names, state abbreviations, city names, and the like.....I really want to make the scan case-insensitive to the case format of the keyword except in the case that the keyword is an abbreviation (i.e., CA, IN, NY,etc. since making this case-insensitive would pick up a lot of garbage, especially in cases like: IN)+_.* As of right now, this is what I am using for my regex(and the results are all over the place):
    *String regex = "[/W]+"+"(?i)"+keyword+"[/W]+";*
    This just seems wrong! I've played around with it and, lets just say, the results have generally gotten worse. Does anyone have any ideas on how I should correctly format the regex to achieve the goal explained above?? Any and all help would be greatly appreciated. Thanks.

    A technique I find very useful in writing complex regex's is to split the regex into its parts and work on just the one part at a time. This helps me to easily see that the components match exactly what I think I am matching. Once I have every individual part working it is very easy to join them to create the entire match.
    Something like...
    String keywordRegex = "";
    String nonAlphaCharRegex = "";
    //..etc. etc
    String fullRegex = keywordRegex + "whateverIwantToMatchBetween" + nonAlphaCharRegex + ...

  • Help using regex to change strings

    I'm writing a utility to move sub folders from computer to computer. I am trying to get regex to work so a string like "d:\\NewScans\\22102\\7-17" would become "\\\\inv108\\data\\users\\clipper\\scan\\22102\\7-17"
    so basically "d:\\NewScans\\" needs to become
    "\\\\inv108\\data\\users\\clipper\\scan\\" Here is the program I wrote taht accepts the input and out put directories like:
    java Paper mover d:\\NewScans \\\\inv108\\data\\users\\clipper\\scan
    This is the code I wrote but it doesn't work:
    /** Created to move files from a remote bureau to MPCB */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.io.OutputStream.*;
    import java.util.regex.*;
    public class PaperMover{
         private File start_path;
         private File destination;
         public static void main(String[] args){
              if(args[0] != null){
                   PaperMover m = new PaperMover(args[0].trim(), args[1].trim());
                   m.move_papers();
              else{
                   System.out.println("no input directory");
                   System.exit(0);
         public PaperMover(String path, String destination){
              start_path = new File(path);
              this.destination = new File(destination);
         public void move_papers(){
              File[] listing = start_path.listFiles();
              File temp;
              String pat = start_path.getAbsolutePath();
              Pattern P = Pattern.compile(pat);
              for(int i = 0; i<listing.length; i++){
                   //get the directories IE 20123
                   try{
                        if(listing.isDirectory()){
                             String t = listing[i].getAbsolutePath();
                             System.out.println(t);
                             String t2 = P.matcher(t).replaceAll(destination.getAbsolutePath());
                             System.out.println(t2);
    I get string t and t2 to be equal.

    I found my troubles!!

  • Generate best matching regex/rule from string set

    does there exist a tool which:
    given a set of strings, will parse those strings and output a the simplest possible regex which matches those strings
    or
    does there exist a tool which, given a regex, can output a simplified(bonus points for optimal) version of the same regex
    ?

    i should rephrase, simplest regex that matches a set
    of strings, and nothing more. i can make some for the
    set: "doggy, dogma, man" -> doggy|dogma|man but if
    my set of strings is very large it might be better to
    try to write the regex like this: dog(gy|ma)|man
    so, the real problem is that i have several sets of
    500+ strings, i need to write a short/efficient regex
    to match them.What do you need it for?

  • Regex to split a String

    Hi,
    Here's a little regex problem Jos e-mailed me the other day, to which I didn't find a satisfactory answer (I found one, but it's rather verbose). Chances are that there is no short (or simple) solution, but when this is the case with a certain problem, I usually know why there is no simple solution because of some limitation with the regex flavor I am using. However, I can't think of one with this problem.
    Also note that this problem can be easily (and far more efficiently) solved by writing a custom method, but I happen to like regex and am curious to know if there's some solution to this I missed.
    So, without further a due, here is the questions:
    Split a String T into parts with a maximum size N without splitting T on a certain sub string S. In other words: try to split a String in as large as possible parts (equal or less than N) without splitting it on a certain sub string.
    You can use only one split(...) call!
    Lets say S = abc and N = 5 then here are a couple of examples:
    T            = xyabcdefgabc
    T.split(...) = [xyabc, defg, abc]
    T            = xyzabcbbzzzabcabcbcacbyyy
    T.split(...) = [xyz, abcbb, zzz, abc, abcbc, acbyy, y]
    T            = xyzzzzabcbabczabcabcabcacbyyy
    T.split(...) = [xyzzz, zabcb, abcz, abc, abc, abcac, byyy]

    uncle_alice wrote:
    Okay, I give up. I can see how to do this with find(), but not with split(). I hope you haven't been waiting for me all this time... :DTo be frank, yes I have. You, sabre and maybe Darryl (in a good mood ; )). Ah well, I am now convinced I didn't overlook some easy shortcut.
    This is what I've cooked up:
    class Main {
        public static void main(String[] args) {
            String[] tests = {"xyabcdefgabc", "xyzabcbbzzzabcabcbcacbyyy", "xyzzzzabcbabczabcabcabcacbyyy"};
            String sub = "abc";
            int max = 5;
            String regex = String.format("(?<=\\G.{%d,%d})(?=%s)|(?<=\\G.{%d})", max-sub.length()+1, max-1, sub, max);
            System.out.println("regex = "+regex+"\n");
            for(String test: tests) {
                System.out.println(test);
                System.out.println("  -> "+Arrays.toString(test.split(regex))+"\n");
    }B.t.w. uncle_alice, this *\G* (previous match) functionality, is this some Java thing or does it exist in other regex flavors as well?

  • Search and Replace String throwing the wrong error message with regexp?

    This came up in a LAVA thread, and I'm not sure if there's a bug here or not. When using Search and Replace string, and using a regular expression of [(G[b|i])], LabVIEW throws error -4622, "There is an unmatched parenthesis in a regular expression."  There are obviously no unmatched parenthesis in that expression, so it seems to me that the wrong error is being thrown. I'm just not sure if that's a syntactically valid regexp. The problem seems to be with nesting [ ]'s inside ( )'s inside [ ]'s. I've tried a couple of regexp resources on the Web, and one suggests it's valid, while the other seems to think it isn't.
    Message Edited by eaolson on 03-13-2007 10:33 AM
    Attachments:
    ATML_StandardUnit2.vi ‏10 KB
    regexp.png ‏5 KB

    adambrewster wrote:
    I think your regexp is invalid.
    In regexps, brackets are not the same as parentheses.  Parens are for grouping, while brackets are for matching one of a class of characters.  Brackets can not be nested.
    If the regexp is replaced with [G[bi]], there is no error, so it's not a matter of nested brackets. I couldn't find anything on the PCRE man page that forbids nested brackets specifically, but it makes sense.
    Your expression "[(G[bi])]", therefore parses as a character class which matches '(', 'G', '[', 'b', or 'i' followed by an unmatched paren, and an unmatched bracket.
    I don't believe that's the case. Replace the regexp with [(Gbi)], and the error goes away. So it's not a matter of the '(' being literal, and then encountering a ')' without a matching '('.
    daveTW wrote:
    what string exactly you want to replace? I think the round braces are not right in this case, since they mark partial matches which are given back by "match regular expression". But you don't want to extract parts of the string, you want to replace them (or delete, with empty <replace string>). So if you leave the outer [( ... )] then your RegEx means all strings with either "Gb" or "Gi".
    It's not my regular expression. A poster at LAVA was having problems with one of his (a truly frightening one), and this seemed to be the element that was causing the problem. I'm pretty sure that the originator of the regexp meant to use G(b|i), which seems like a complicated way of matching "Gb" or "Gi", if you ask me.

  • Regex with xml for italicize or node creation

    Okay
    Guess it's a complex situation to explain.
    I am working on the text content of xml documents again. made quite a lot of progress with some of my other regex requirements.
    I am looking for a specific set of words to italicize say for example 'In Vitro'
    String Regex = "In Vitro";
    // here I get the text of a particular xml Node which is a text node
    String paragraph = nl.item(i).getNodeValue();
    //Value of paragraph before replace is "and lipids and In Vitro poorlysoluble(in water"
    String replace = "<Italic>In Vitro<Italic/>";
    String paragRepl = m.replaceFirst(replace);
    //Value of pargRepl after regex replace is "and lipids,?;:!and <Italic>In Vitro<Italic/> poorlysoluble(in water"
    //then I update the content of the node again
    nl.item(i)..setNodeValue(paragRepl);
    // save the xml documentthe italic tag is interpreted by our custom stylesheet to display "In Vitro" in italics, the reason it cannot do that is because the the character entities of the < and > have been put in the text content of the node i.e &lt; and &gt;. On closer examination of the text of the node after the document was saves, it appeared this way " &lt;Italic>In Vitro&lt;Italic/> ". For some reasom the greater than sign came out okay, but still no point, It didn't actually create a new node. I am not sure how you can automatically put tags around specific text you find in xml documents using regex, or If I have to create a new node at that point.
    it's xml so these entities come into picture.
    any help is greatly appreciated, in short I need to just add a set of tags to a particular regex I find in an xml document,
    thanks in advance
    Jeevan

    okay i am getting closer to the solution as there is an api call from another proprietary language that would do this
    but as I loop through the xml document, it keep selecting the text "In Vitro" even after it has been italicized.
    So I guess my next challenge is getting a regex which looks for "In Vitro" but not italicized
    For regex so far I have seen case insensitive handling, I have seen for italics
    basically if I I can get my hands on a regex for example
    String regex = "In Vitro && Not Italic"
    any help is appreciated
    Jeevan

  • Parse html (a href) using regex

    Hello,
    i would like to extract all the urls from a website that are included in < a href=" parse string">
    I have already the regex which is
    String regex = "< *a.*href *= *['|\"]";
    May you please advise me which method in Pattern or Matcher classes shall i use in order to take as output
    *only* the url inside the " " marks?
    I have already tried end and start methods which return the indexes, but i don't get the desirable result.
    Thanks, in advance
    P.S.Also, i have already tried to use HtmlParser but i prefer to use regex cause i found a difficulty in it.

    Please continue in your original thread.
    [http://forums.sun.com/thread.jspa?threadID=5363751]

  • 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?

  • Java + Regex (Searching one or more words within quotes)

    Hi Everyone,
    I have the following text:
    The brown "cow" jumped over the "contributor licensing agreement" and went to "Burger King" to order chicken wings.
    The text above may change. This is the input text. In the input text, I want to find and print out all phrases that are inbetween quotes. I tried the following:
    Pattern pattern = Pattern.compile("\"(.*)\"");
    Matcher matcher = pattern.matcher(paragraph);
    while(matcher.find()){
        String theMatch=matcher.group();
        System.err.println("Found: "+theMatch);
    }The regex expression works in my regex coach program (which helps me test regex on input strings). In the Java code, however, the application only finds "cow", not "contributor licensing agreement" or "Burger King." What do I need to do in order to find those other two phrases? I want to use this to parse out and find all words or phrases in a document that are inbetween quotes.
    What am I doing wrong??
    jetcat33

    how about using a "reluctant" quantifier "?"
        Pattern pattern = Pattern.compile("\".*?\""); // note the question mark
        Matcher matcher = pattern.matcher(paragraph);
        while(matcher.find()){
            String theMatch=matcher.group();
            System.err.println("Found: "+theMatch);
        }

  • Remove of non-printable characters from string

    Hi Gurus,
    How can I achive that? I have a string in which sign "end of line" occurss. How can I delete them?
    BR
    Marcin Cholewczuk

    Hi Marcin,
    Just use a [REPLACE|http://help.sap.com/abapdocu_70/en/ABAPREPLACE.htm] with a regular expression (Assuming variable STRING holds your data):
    replace all occurrences of regex '[\n\r]+' in STRING with ''.
    Note that I replaced newline ('\n') - also called end of line - and carriage return ('\r') with nothing (matching your delete request). This might not be wanted if you have a true multiline string. If you're more paranoid and want to cover more cases, you might want to replace any non-displayable characters using '\[\[:print:\]\]+' as your search pattern. So if you want to replace non-displayable characters with a space and just kill it at the end of the string you could use something like this:
    replace all occurrences of regex '[^[:print:]]+(?!$)' in STRING with ` `.
    replace all occurrences of regex '[^[:print:]]+$' in STRING with ''.
    Cheers, harald

  • Regex, jdbc help needed

    I need to write a program that extracts data from a website an inserts it into a oracle database. I use HTML or JTidy to pretty the html code of the site that I am going to extract. After prettying up the code us JTidy, I save file in a tempory directory. I then would like to use regex to find and extract data between two points and multiple lines.
    example:
    <table>
                   <!-- project title -->
                   <tr>
                        <td width="30%" align="right" valign="top">
                             <font size="-1">
                             <b>Project Title:</b>
                             </font>
                        </td>
                   <td width="70%" align="left" valign="top">
                             <font size="-1">
                             A Broad Spectrum Catalytic System for Removal of Toxic Organics from Water By Deep Oxidation
                             </font>
                        </td>
                   </tr>
    first I would like to find the place holder<b>Project Title:</b> then extract the data between
    <font size="-1">
                             A Broad Spectrum Catalytic System for Removal of Toxic Organics from Water By Deep Oxidation
    </font>m and insert it into the oracle db
    How do I do this? Once I am done with the webpage I can delete it from the temp directory. I have code that pretties the html code, and a java app that extracts data between two points. The only problem is that it has to have the same regex input. It will extract the data if the tags are identical like
    <b>data between these tags<b>, works but
    <b>data between these tags </b> does not. I then think I need to write a prepared or just statement to load the data into the db?
    orozcom

    That is not exactly what I need. This is a testing class that I have been playing with.
    import java.util.regex.*;
    public final class SplitTest
    private static String REGEX = "<";
    private static String INPUT = "<p>one two three four</p>five";
    public static void main(String[] argv)
    Pattern p = Pattern.compile(REGEX);
    String[] items = p.split(INPUT);
    System.out.println( "Length of array items: " + items.length);
    System.out.println( "Item 0 of array items: " + items[0]);
    System.out.println( "Item 1 of array items: " + items[1]);
    System.out.println( "Item 2 of array items: " + items[2]);
    System.out.println( "Item 3 of array items: " + items[3]);
    //for(int i=0;i<items.length;i++)
    for(int i=1;i<2;i++)
    System.out.println(items);
    items = null;
    The out put is :
    Length of array items: 3
    Item 0 of array items:
    Item 1 of array items: p>one two three four
    Item 2 of array items: /p>five
    why array item 0 empty? I would like to read between two html tags. I found some examples (sort of ) at http://www.regular-expressions.info/examples.html. It gave me the below information, but I still dont know how to implement.
    <TAG[^>]*>(.*?)</TAG> matches the opening and closing pair of a specific HTML tag. Anything between the tags is captured into the first backreference. The question mark in the regex makes the star lazy, to make sure it stops before the first closing tag rather than before the last, like a greedy star would do. This regex will not properly match tags nested inside themselves, like in <TAG>one<TAG>two</TAG>one</TAG>.
    any Ideas?

Maybe you are looking for

  • Can I stop Windows 7 from warning about security on every linked PDF?

    I am working on a project for which I have created one central document (an index) and 120 linked documents. Each document, on every page, has links to the next alphabetical document and back to the central index. This project will be distributed on

  • Did something change in Adobe Reader X with xfdf files?

    Has something changed with Reader X and how to print .xfdf files?  For several previous versions my program would use reader to print pdf forms with data filled in via .xfdf files.  Once someone updates to ReaderX  then the pdf's print but no data is

  • Service copied for contract to Purchase Order

    Deal all, when i adopt service from contract to purchase order the quantity is automatically changed to 1 always, whether i have defined the quantity of service more than 1. Please give the solution why this is happening, is this the standard setting

  • Mainstage 2.1.2 Question

    Not sure what to do with this. When I assign my Keystation 88 to Mainstage I am able to see all of my splits, layers, etc. However, when I assign the input to Keyboard 1 (another midi input assigned to my Kurzweil) I lose the graphic of my splits and

  • How to add sound to a button in storyboards on xcode Version 4.6.2 (4H1003)?

    i am running xCode Version 4.6.2 (4H1003) on my macbook air with mountain lion. i am trying to add sound to a button that is in the scroll view but i cant? PLEASE I NEED HELP ASAP!!!