[regex help]Find EXACT characters in a String.

Ok, i got this so far ...
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Testing2 {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("[wati]");
        String text = "water";
        Matcher m = p.matcher(text);
        if (m.find()) {
            System.out.print("Found !");
}With that code i got a match, but i ONLY want to find a match if the String matches the EXACTS characters in the Pattern ...
in this case i got, 'w' 'a' 't' 'i', 'w' 'a' 't', are in water, but 'i' NOT, so i don't want to have a match !
i don't know how to acomplish this :(
hope you can help me, thanks in advance :D

Username7260 wrote:
Ok, i got this so far ...
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Testing2 {
public static void main(String[] args) {
Pattern p = Pattern.compile("[wati]");
String text = "water";
Matcher m = p.matcher(text);
if (m.find()) {
System.out.print("Found !");
}With that code i got a match, but i ONLY want to find a match if the String matches the EXACTS characters in the Pattern ...
in this case i got, 'w' 'a' 't' 'i', 'w' 'a' 't', are in water, but 'i' NOT, so i don't want to have a match !
i don't know how to acomplish this :(AFAIK there's no syntax in regular expressions to accomplish this. Try turning your string into a character array, sorting it, and then do a simple comparison.

Similar Messages

  • How can I find special characters in a string

    Hi,
    I have a small task where I have to find special characters in a given string, Can anyone suggest.......................

    What is your definition of a "special character", could you be a bit more specific?
    Do you simply want to find the position of a specific character in the string?
    Can it occur more than once and you want to find all occurences?
    What should happen if the special character does not exist?
    For programming purposes, all 256 possible 8 bit characters (x00-xFF) can be treated the same. Non-printing characters can be entered in \-codes or hex display if needed.
    LabVIEW Champion . Do more with less code and in less time .

  • Help finding key characters in txt file

    Hey I have just been trying to code a script that I can use to search through a bunch of rankings (like 1.Jorden 2.Honza 3.Bill 4.Ted etc) and find how many times each player recieved a vote for each ranking.
    So I might end up with, Jorden was voted 1st 9 times, 2nd 3 times and 3rd 0 times.
    The votes are in a text document, here is the script so far:
    public static void printVotes() {
         int count = 0;
         int a = 0;
         File nFile = new File("Sick1.txt");
         FileReader inStr = new FileReader(nFile);
         BufferedReader inBuff = new BufferedReader(inStr);
         for(int i = 1; i < 15; i++) {
              while(a == 0) {
                   String txtName = inBuff.readLine();
                   if(txtName != null) { //txtName holes the line IE if its like this 1.) Honza
                        StringTokenizer placing = new StringTokenizer(txtName, i);
                        StringTokenizer name = new StringTokenizer(txtName, "Honza");
                        if(placing != null && name != null) {
                             count++;
                   } else {
                        System.out.println(count);
                        a = 1;
    Now it is far from perfect (that being the main reason it does'nt work) but my key question is:
    what is the best way to check how many lines in the .txt document contain the int i (from the for loop) and a givin name (eg "Honza")?

    Your logic needs to look like this:
    for (each line)
       split the line into the rank and name;
       Increment the count for the name/rank pair.
    }I would use a Map to store the rank counts for each name found.

  • Finding special characters in a string

    Hi all, I'm using Oracle 10g.
    I have a column name with special characters. how can i replace the special characters in the column with space and no space depending on the special character. please see the example below. can i use REGEXP_REPLACE?
    create table
    create table sample_test (
      Name    VARCHAR2(20 BYTE))insert table
    insert into sample_test values ('O''NEIL')
         insert into sample_test values ('B.E.VICTOR')
          insert into sample_test values ('WILLIAM,L')
           insert into sample_test values ('MARY-ANNE')
               insert into sample_test values ('VON_ANCKEN')
                insert into sample_test values ('BROWN;L')i would like the output as follows
    Special Character         Name         Expected Name                  Remarks     
    Single Quotes            O'NEIL                  ONEIL                          Remove quotes and no space       
    Dot                      B.E.VICTOR         B E VICTOR                          Replace with space       
    Comma                      WILLIAM,L         WILLIAM L                          Replace with space       
    Hyphen                      MARY-ANNE         MARY ANNE                          Replace with space       
    Underscore                VON_ANCKEN         VON ANCKEN                          Replace with space       
    Semi Colon              BROWN;L          BROWN L                          Replace with space      Please advise.
    Thanks
    Bob

    A simple solution without regexp usage:
    -- Test Data
    with sample_test as
    select 'O''NEIL' name from dual union all
    select 'B.E.VICTOR'  name from dual union all
    select'WILLIAM,L' name from dual union all
    select'MARY-ANNE' name from dual union all
    select'VON_ANCKEN' name from dual union all
    select'BROWN;L' name from dual
    -- Query:
    select name old_name,
           translate(replace(name,'''',''),'.,-_;','     ') new_name
    from sample_test;Explanation:
    REPLACE deletes the quote
    TRANSLATE replaces dot, comma, hyphen, semicolon and underscore

  • Help finding exact model #

    Hello, I understand that many Big Box stores use their own part numbers.  I have a Satellite that I need to replace the screen on.
    It is a Satellite pro c850 part # PSKC96-00X00P
    I see many replacement screens for c850's but they all have 3 digits appnded to the end of c850
    Thank you.
    Solved!
    Go to Solution.

    Satellite pro c850 part # PSKC96-00X00P
    Just to put you on the right track, it must be this model.
    Satellite Pro C850-00X (PSKC9C-00X00P)
    This site is for US computers. Good luck with that Canadian model.
    BTW, what counts is the part-number family. What you find for PSKC9x should work for any x.
    -Jerry

  • Finding multiple characters with IndexOf.

    I was wondering how I could find mutiple characters in a String using IndexOf, or anything else that might work.
    An example would be wanting to find the first position of any vowel.
    thanks in advance.

    ok, heres some code. The whole point of this method is find the first occurence of a,e,i,o, or u. The greater cause is a PigLatin translator.
         public static String findFirstVowel(String input)
              int word;
              word = input.indexOf('a'); //this is where I think the problem lies
              if(word > 0)
                   System.out.println(word) //just for debugging purposes
                   return word + "";
              return "";
         }//findFirstVowel()

  • Regex help please...need to grab a part of a string...

    Hi there,
    I have a dynamic form with dynamic fields and dynamic data types.
    What I want to do is check the data-type by grabbing it from the field name.
    For example, I have a field name called "formEmail[email]"
    The whole string is the field name, but I want to also extract the string between the square brackets. This will help me validate the field when it comes to submission. I will then compare it to a last of valid possibilities.
    How can I do this? I know it must require some regex but I have never looked into REGEX....my bad I know...but I promise I will in future!
    Mnay thanks,
    Mikey.

    Mikey,
    One question: Is the string 'formEmail[email]' the actual string (as opposed to being a variable that will get evaluated)?
    There are a lot of ways to go about this sort of task with regular expressions. If 'formEmail[email]' is an example of the type of strings you are searching, a regular expression along the lines of the following might be useful:
    <cfscript>
         mystring = "formEmail[email]";
         results = ReMatch( "[(\w*)]+", mystring );
    </cfscript>
    <cfdump var="#results#">
    <cfoutput>
         First match: #results[1]#<br />
         Second match: #results[2]#<br />
    </cfoutput>
    This regular expression finds all words in a string but does not match the brackets. \w is a special character that matches any word character -- including alphanumeric chars and underscores.
    The brackets used in the regular expression are also special characters for groupingt, which you can learn more about in the section of the CF WACK book mentioned JR's reply.
    If you run this snippet, you'll notice you get an array back. The first element of the array is the first match of the regexp, or the string 'formEmail', and the second element of the array is the second match of the regexp, or the string 'email'.
    This regular expression might be helpful if you don't have consistent text before the brackets but you know the string you'll search is formatted as: string[string].
    For example, the ReMatch expression above works the same for either of the strings below:
    formPhoneNumber[phone] (would return two strings: formPhoneNumber and phone)
    formEmail[email] (would return two strings: formEmail and email)
    As you delve into the world of Regular Expressions, be sure to grab RegExr, an Adobe AIR application for testing regular expressions. Very handy, especially coupled with a reading of the RegExp section in Forta's CF WACK book!
    http://gskinner.com/RegExr/desktop/

  • How to find out if there are repeated characters in a string

    hey guys
    well i kinda have a little problem figuring out how to find out
    if there are repeated characters in a string.
    if anyone can help, would appreciate it.
    thanks
    milos

    Try using the StringTokenizer class. if u already know which character to trace. this could do the job.
    eg. String str = "here and there its everywhere";
    StringTokenizer st = new StringTokenizer(str, "e");
    int rep = st.countTokens();

  • Using Regex to find a string

    Is it viable to use regular expressions with Search "Specific Tag" (i.e.script) "With Attribute" (i.e. src) and "=" a regex string rather than an exact search term?  The Find / Replace tool seems to prevent a simple wildcard find (i.e. "searchter* for searchterm) Any insight would be appreciated.

    uneAaron wrote:
    I'm trying to replace a script source value for all instances where the value contains the string "/bbcswebdav/xid-144702_1", where the string "xid-" is followed by a different numerical value in each instance.
    The regex for finding that string is:
    \/bbcswebdav\/xid\-[\d_]+
    The numerical value contains an underscore, so the final section ( [\d_]+ ) uses a range that selects one or more numbers and/or underscores.
    Perhaps as important as identifying what you want to find is specifying how you want to replace it. Regexes can have capturing groups that can be used in the Replace field to simplify matters.

  • Find no numbers  characters in a string

    HI, how i can find no numbers characters in a string
    By example.
    '45125454564#4545'.
    I only want to know if the string contains no number characters or not, i dont want to know the character '#'.
    Is there a function module for doing that?
    Thanks

    You can try:
    NUMERIC_CHECK
    OR
    IF var CO '0123456789'.
    ENDIF.

  • Need help in replacing special characters in a string

    Hi,
    please let me know the best way to replace all the special characters in a string with space.
    other than alphabets and numbers
    with regards.
    sumanth.

    please let me know the best way to replace all the special characters in a string with space.
    other than alphabets and numbers
    >
    Sumanth Nag Kristam wrote:
    > actually i need to replace hexa decimal char 0X1A in a string.... that is 'substitue' as per the chart
    > any pointers....
    >
    > chk the link for the ASCII codes
    > http://www.techonthenet.com/ascii/chart.php
    But in Hexa decimal value there is no special characters?

  • RegExp for excluding special characters in a string.

    Hi All,
    Im using Flex RegExpValidator. Can anyone suggest me the correct expression to validate this condition?....
    I have tried this expression :----- /^[^///\/</>/?/*&]+$/...But in this it is also negating the alphabets.Also I have tried with opposite condition that in the String we should have alphabets and the expression is:-- ([a-z]|[A-Z]|[0-9]|[ ]|[-]|[_])*..... Please can anyone help me on this.
    Thanks in advanced to all.
    Munira

    sorry but you are posting things back that do not make any sense
    what do you mean with the below comment?
    munira06 wrote:
    Yes you are correct ,but I have tried this with single special character
    say
    Re: RegExp for excluding special characters in a string.
    here is a sample app taken from the live docs
    using ^[a-zA-Z0-9 \-_]*$ as the regex accepts all characters from a-z, A-Z, 0-9 - [space] and_
    run the example tell me what regex you are using and what test strings fail when they should pass or pass when they should fail
    <?xml version="1.0" encoding="utf-8"?>
    <!-- Simple example to demonstrate the RegExpValidator. -->
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/mx">
        <fx:Script>
            <![CDATA[
                import mx.events.ValidationResultEvent;
                import mx.validators.*;
                // Write the results to the
                private function handleResult(eventObj:ValidationResultEvent):void {
                    if (eventObj.type == ValidationResultEvent.VALID) {
                        // For valid events, the results Array contains
                        // RegExpValidationResult objects.
                        var xResult:RegExpValidationResult;
                        reResults.text = "";
                        for (var i:uint = 0; i < eventObj.results.length; i++) {
                            xResult = eventObj.results[i];
                            reResults.text=reResults.text + xResult.matchedIndex + " " + xResult.matchedString + "\n";
                    } else {
                        reResults.text = "";
            ]]>
        </fx:Script>
        <fx:Declarations>
            <mx:RegExpValidator id="regExpV"
                    source="{regex_text}" property="text"
                    flags="g" expression="{regex.text}"
                    valid="handleResult(event)"
                    invalid="handleResult(event)"
                    trigger="{myButton}"
                    triggerEvent="click"/>
        </fx:Declarations>
        <s:Panel title="RegExpValidator Example"
                width="75%" height="75%"
                horizontalCenter="0" verticalCenter="0">
            <s:VGroup left="10" right="10" top="10" bottom="10">
                <s:Label width="100%" text="Instructions:"/>
                <s:Label width="100%" text="1. Enter text to search. By default, enter  a string containing the letters ABC in sequence followed by any digit."/>
                <s:Label width="100%" text="2. Enter the regular expression. By default, enter ABC\d."/>
                <s:Label width="100%" text="3. Click the Button control to trigger the validation."/>
                <s:Label width="100%" text="4. The results show the index in the text where the matching pattern begins, and the matching pattern. "/>
                <mx:Form>
                    <mx:FormItem label="Enter text:">
                        <s:TextInput id="regex_text" text="xxxxABC4xxx" width="100%"/>
                    </mx:FormItem>
                    <mx:FormItem label="Enter regular expression:">
                        <s:TextInput id="regex" text="ABC\d" width="100%"/>
                    </mx:FormItem>
                    <mx:FormItem label="Results:">
                        <s:TextInput id="reResults" width="100%"/>
                    </mx:FormItem>
                    <mx:FormItem >
                        <s:Button id="myButton" label="Validate"/>
                    </mx:FormItem>
                </mx:Form>
            </s:VGroup>
        </s:Panel>
    </s:Application>

  • Check special characters in a string

    Hi all
    I am using oracle 10g.......
    Need to know what should be the query to check whether a special character exists or not in a column value....used the following query like.....
    select count(*) into emp_no_count_special_char
    from dual
    where REGEXP_LIKE(insert_data_rec.emp_no , '[#!$^&*%./\|]$' )
    or REGEXP_LIKE(insert_data_rec.emp_no , '^[#!$^&*%./\|]' );
    This works fine in case a string starts or ends with a special character ,what if a string of special characters lies in between numeric digits.
    e.g: '100#$%7'
    Please help find a query that checks for the existence of sp. characters irrespective of their position in the column!!!
    Thanks
    Dave
    Edited by: Dave on Jun 6, 2012 5:17 AM
    Edited by: Dave on Jun 6, 2012 5:18 AM

    Hi Dave,
    example below:
    -- Check that at least one special character exists in the string
    SELECT COUNT (*)
      INTO emp_no_count_special_char
      FROM DUAL
    WHERE REGEXP_LIKE (insert_data_rec.emp_no, '[#!$^&*%./\|]');
    -- Check that no special characters exist in the string
    SELECT COUNT (*)
      INTO emp_no_count_special_char
      FROM DUAL
    WHERE NOT REGEXP_LIKE (insert_data_rec.emp_no, '[#!$^&*%./\|]');
    {code}
    Regards.
    Al                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Scanning values of characters in a string

    Hey guys,
    I'm trying to figure out a way I can scan the individual characters of a string to find their value [unicode value, if it's a number, punctuation etc] and I'm a bit stumped. I know I'll have to be using the chatAt method, but I can't think of how I could implement this to continuiously scan through a string.
    If someone could help point me in the right direction it would be great.
    Thanks in advance.

    Do you know what a loop is?
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html
    Do you know how to find out about methods of the String class such as toCharArray?
    http://java.sun.com/javase/6/docs/api/java/lang/String.html

  • Replace characters in a string

    Hi,
    I need to replace all occurrences of control characters except space,newline,tabs in a string . I can give a replace statement for each of these characters but I want to avoid this by making use of regular expressions. Can anyone help me in this regard.
    I tried using the following replace statements with regular expression, but i am not getting the required results:
    replace all occurrences of REGEX '[[:cntrl:]]' in lv_char with space replacement count lv_count_r.
    ---> this replaces even the spaces
    replace all occurrences of REGEX '[[:cntrl:]][^[:space:]]' in lv_char with space replacement count lv_count_r.
    --> this replaced even some alpha numeric characters
    Thanks and Regards,
    Shankar

    is there anyway to do this without using regular
    expressions.. regular expressions are the last
    solution for me..Remember that you can never really replace the characters of a String. Strings are immutable. Once created they cannot change.

Maybe you are looking for