Find and print illegal character in a string using regexp

I have the following simple regexp that checks for illegal characters in a string:
String regexp = ".*([\\[\\];]+|@).*"; // illegal: [ ] ; @
String input = "Testing [ 123";
System.out.println(Pattern.matches(regexp, input));How can I find and print the character that is invalid??
I've tried using the Matcher class togheter with Pattern but cant get it to work. :(
Like this:
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(input);
matcher.lookingAt();
int matchedChar = matcher.end();
if (matchedChar < input.length()) {
    String illegalCharFound = String.valueOf(input.charAt(matcher.end()));
    System.out.println(illegalCharFound);
}What am I doing wrong?

1. You call lookingAt(), but you don't check its return value, so you don't know if the regex actually matched.
2. matcher.end() returns the index of the character following whatever was matched, assuming there was a match (if there wasn't, it will throw an exception). So either it will point to a legal character, or you'll get a StringIndexOutOfBoundsException because an illegal character was found at the end of the input. The start() method would be a better choice, but...
3. Your regex can match multiple consecutive square brackets or semicolons (and why not put the at-sign in the character class, too?), but you're acting like it can only match one character. Even if there is only one character, group(1) is an easier way to extract it. Also, if there are more than one (non-consecutive) illegal characters, your regex will only find the last one. That's because the first dot-star initially gobbles up the whole input, then backtracks only as far as it has to to satisfy the rest of the regex. If your goal is to provide feedback to whoever supplied the input, it's going to be pretty confusing feedback. You should probably use the find() method in a loop to pick out all the illegal characters so you can report them properly.

Similar Messages

  • HT4356 My iPad does not find the printer. I have tried the HP app and it works, but in any other app the printer is not seen. How do I get this printer visible to the iPad. All my other network computers find and print with out a problem.

    My iPad does not find the printer. I have tried the HP app and it works, but in any other app the printer is not seen. How do I get this printer visible to the iPad. All my other network computers find and print with out a problem.

    hi
    could you tell us what the other app is please

  • I am a 66 year old novice. How can I find and print all the functions of the cmd key, please.

    How can I find and print out all the functions of a command cmd key please?

    You can also  go to MAC HELP and enter 'Key Board Shortcuts' in the entry field.
    Ciao.

  • How can I find and print a bill from Apple site?

    Hi
    I need to get and print the invoice of my macbook purchase. It's a MacBook Pro I bought in 2008 and as It is an old purchase it makes it quite difficult to find the invoice.
    Thank you for help.

    If Apple doesn't keep invoices that old (and I wouldn't expect them to), and you didn't keep it either, there's probably no way to get it.  What do you need from the invoice?  Note that if you just need the serial number, and don't have the machine available, you can view that by logging in here:
    https://supportprofile.apple.com
    It's possible such an old machine might not be listed there, though.

  • Finding the occurences of | character in a string

    Hi There,
    I want to restrict the occurrences of | character in a string to some number. How can I make this check using regular expressions.
    Thanks,
    --JJ                                                                                                                                                                                                                                                                                                                               

    sabre150 wrote:
    guitar_man_F wrote:
    sabre150 wrote:
    jose wrote:
    So can you please suggest me a sample regex which will do this.
    --JJErr.. err.. err.. reply #2?Not quite. I believe he said restrict it to a certain max number of |, not remove all instances. I think.
    Look again. I remove anything that is NOT a '|' char and then count what is left.Oops! My bad! You're right. That would work.
    - Adam

  • How to find out the repeated character in a string value?

    Dear People,
    I want to trace out the names where a character occured more than once.for ex, i need o/p as
    ENAME
    ALLEN
    TURNER
    ADAMS
    In which all the above name consists of repeated characters.
    I use Oracle 10g and i tried using REGEXP say for ex,
    SELECT ENAME FROM EMP WHERE REGEXP_LIKE(ENAME,'L{2}');
    ENAME
    ALLEN
    MILLERbut this works only for single character.how to specify condition for any character?.pls suggest me.
    With Regards
    VIDS

    Here is one way you can use from version 10 upwards:
    SQL> with emp as
      2  ( select 7369 empno, 'SMITH' ename, 'CLERK' job, 7902 mgr, date '1980-12-17' hiredate, 800 sal, NULL comm, 20 deptno from dual union all
      3    select 7499, 'ALLEN', 'SALESMAN', 7698, date '1981-02-20', 1600, 300, 30 from dual union all
      4    select 7521, 'WARD', 'SALESMAN', 7698, date '1981-02-22', 1250, 500, 30 from dual union all
      5    select 7566, 'JONES', 'MANAGER', 7839, date '1981-04-02', 2975, NULL, 20 from dual union all
      6    select 7654, 'MARTIN', 'SALESMAN', 7698, date '1981-09-28', 1250, 1400, 30 from dual union all
      7    select 7698, 'BLAKE', 'MANAGER', 7839, date '1981-05-01', 2850, NULL, 30 from dual union all
      8    select 7782, 'CLARK', 'MANAGER', 7839, date '1981-06-09', 2450, NULL, 10 from dual union all
      9    select 7788, 'SCOTT', 'ANALYST', 7566, date '1982-12-09', 3000, NULL, 20 from dual union all
    10    select 7839, 'KING', 'PRESIDENT', NULL, date '1981-11-17', 5000, NULL, 10 from dual union all
    11    select 7844, 'TURNER', 'SALESMAN', 7698, date '1981-09-08', 1500, 0, 30 from dual union all
    12    select 7876, 'ADAMS', 'CLERK', 7788, date '1983-01-12', 1100, NULL, 20 from dual union all
    13    select 7900, 'JAMES', 'CLERK', 7698, date '1981-12-03', 950, NULL, 30 from dual union all
    14    select 7902, 'FORD', 'ANALYST', 7566, date '1981-12-03', 3000, NULL, 20 from dual union all
    15    select 7934, 'MILLER', 'CLERK', 7782, date '1982-01-23', 1300, NULL, 10 from dual
    16  )
    17  select ename
    18       , e
    19       , count(*)
    20    from ( select ename
    21                , e
    22             from emp
    23            model
    24                  return updated rows
    25                  partition by (ename)
    26                  dimension by (0 i)
    27                  measures (ename e)
    28                  ( e[for i from 1 to length(e[0]) increment 1] = substr(e[0],cv(i),1)
    29                  )
    30         )
    31   group by ename
    32       , e
    33  having count(*) > 1
    34  /
    ENAME  E        COUNT(*)
    SCOTT  T               2
    MILLER L               2
    ADAMS  A               2
    ALLEN  L               2
    TURNER R               2
    5 rows selected.Regards,
    Rob.

  • Can i find and print the history through the firefox browser from a mac?

    I am trying to print recent history of the sites visited from a Macintosh laptop. I can not figure out how to open up the history and print it. I was wondering if this was possible?

    This is how you can print Firefox history (on MacOS but should work on other platforms)
    1) Download and install SQLite Manager FF plugin
    2) Open SQLite Manager from the tools menu
    3) Open places.sqlite from your-home/Library/Application Support/Firefox/profiles/your.profile/
    4) Hit the Execute SQL tab
    5) Use a select similar to this.....
    <pre><nowiki>SELECT datetime(moz_historyvisits.visit_date/1000000,'unixepoch'), moz_places.url, moz_places.title
    FROM moz_places, moz_historyvisits
    WHERE moz_places.id = moz_historyvisits.place_id AND url LIKE '%youtube%'</nowiki></pre>
    6) Click the actions dropdown to export to CSV
    Hope this helps

  • Replace a character in the string using JavaScript

    Hello,
    I would like to ask for help. I have a field in the xml file, which contains a string, that is bound to a text box on the form.
    What would be the syntax to replace a special character in the string with the carriage return?
    Thank you.

    Huh?
    I didn't post my script...
    It sure is no clean programming since I just took Steve's solutions and delted the ligns I didn't want (the app allert)
    Though in the end I've taken the boolean out, or better replaced it with the replacement function, it repeats the replacement no matter how much "ü"'s are in there. (I needed the boolean though, since as long as it was true, the if should be executed.)
    Somehow it worked... not 100% sure why, but it worked.
    if (findChar(this.rawValue)) {
    function findChar(str)
    {  for (var i=0; i < str.length; i++)
            if (str.charAt(i) == "ü")
               this.rawValue = this.rawValue.replace(/ü/,"ue")

  • Testing the last character of a string using .endsWith

    Hi,
    I want to check to see if the last character of a string is '@'.
    I tried if (StringAt.endsWith("a")) {
    but got no results.
    Is this the correct way to do it or is there another way.
    Thanks.
    Adam

    javadocs:
    String's endsWith method takes a string argument.
    endsWith
         public boolean endsWith(String suffix)
              Tests if this string ends with the specified suffix.
              Parameters:
                   suffix - the suffix.
              Returns:
                   true if the character sequence represented by the argument is a suffix of the character sequence
                   represented by this object; false otherwise. Note that the result will be true if the argument is the
                   empty string or is equal to this String object as determined by the equals(Object) method.
              Throws:
                   NullPointerException - if suffix is null.

  • Running scripts from Finder, and printing without a dialog

    Two questions:
    1. I need to launch an InDesign script from outside of InDesign -- such as launching the script from the finder (Mac) and having it run in InDesign. How can I do this?
    2. How can I script printing a document so that it automatically prints, bypassing the Print dialog box that a user would need to click on (I want it to print automatically in the background without the user doing anything)?

    1. Just save your script as an application and wrap your stuff in an "on run" statement.
    on run
    tell application "Adobe InDesign"
    --do stuff
    end
    end
    See the Applescript Language Reference at developer.apple.com/applescript for more info.
    2. Yup. The "do stuff" in your case will probably look something like this:
    tell active document
    --Set up options, assumes you have created a printer preset
    -- with the output options you want
    set active printer preset of print preferences to "my printer preset"
    set page range of print preferences to all pages
    --And then print
    print without print dialog
    end

  • How to find and print Apple Store and Easy Pay Receipts

    I bought a non-Apple product at the Apple store using Easy Pay.  After 4 months, the product was defective and I needed to print the receipt to send to the manufacturer.  When I found the receipt on my Apple Store app, there was no option to print the receipt.  I went to my Apple ID account in iTunes, but store purchases do not show up there, even though I use the same Apple ID for both.  I ended up taking a few screen shots, but this was not ideal.
    1.  I would like to know if there is a way to print these Easy Pay receipts properly?
    2.  Is there an Apple web location where I can find all my purchases, including all hardware, apps, music, movies, etc., purchased using the same Apple ID but through various different methods, such as Easy Pay, in-store service, on-line, etc? I know when I go to the Apple store the employees can pull up my entire hardware purchase history, but I can't seem to do this on my own.

    1 - having lots of photos will not slow your Mac down
    2 - there are many things that will including lack of hard drive space. How much free space do you have on yoru hard drive?
    3 - duplicates and some triplicates of photos often means that you have imported an iPhoto libraruy into another iPhoto library - you never do that - it does not work and creates a mess
    4 - the best, easiest and safeest way to detect and delete duplicates is to use the paid version of iPhoto Library Manager - http://www.fatcatsoftware.com/iplm/ - 
    LN

  • Find a non-numeric character in a string

    I have a 3 strings (1) 'AB99CDEFGH0012%' (2) 'ABCDEFGH' (3) 'ABCD11'
    how do i find out a that string has non numeric characters
    Thanks

    SELECT TO_NUMBER(:String) FROM DUAL;
    is a quick way to find out :)
    A good method would be.....
    SELECT 1 FROM DUAL WHERE TRANSLATE(:String, 1234567890, '-') IS NOT NULL;
    The statement will filter out ALL numbers, leaving behind any characters, A, B, @, ~, etc...

  • HT204088 how to find and print invoice at itunes

    please help

    Trying to find out the same thing. All they keep sending me are receipts.
    The email sent by iTunes/App Store is not an invoice but a receipt, it doesn't follow any of the legal reuirements for a receipt, there's not even a mention of the VAT amount.
    In App/iTunes store it is impossible to get VAT return which makes it unfavorable for corporations to buy software through Apple's channels.

  • How to extract specific text from a string using REGEXP

    Some- &3*1233 Test Oralce #1`2" pip, various lengths couplers Misc. Metal,,'VENDOR'), PART_NO=5966 DESCRIPTION=INV 564400 2"
    How can I extract text start from 'Test' and end before DESCRIPTION? the results I need is as follows:
    Test Oralce #1`2" pip, various lengths couplers Misc. Metal,,'VENDOR'), PART_NO=5966
    I will apprecaite if you could explain the solution.

    set define off
    with t as(
              select 'Some- &3*1233 Test Oralce #1`2" pip, various lengths couplers Misc. Metal,,''VENDOR''), PART_NO=5966 DESCRIPTION=INV 564400 2"' str from dual
    select  regexp_replace(str,'(^.*)(Test.*)( DESCRIPTION.*$)','\2') result
      from  t
    RESULT
    Test Oralce #1`2" pip, various lengths couplers Misc. Metal,,'VENDOR'), PART_NO=5966
    SQL>
    {code}
    SY.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Newline character in a String

    Hi,
    How can i introduce a newline character while displaying a string using string functions in a report..
    My requirement is, i need to replace a particular character with a newline character in a string using replace function
    Can anyone help me
    Thanks in advance..

    Hi,
    Try this,
    REPLACE(Table.ColumnName, 'StringOrCharacterToReplace', '[br]')
    *replace [ and ] with < and > in br tag. Change the data format of the column to HTML
    Rgds,
    Dpka

Maybe you are looking for

  • Raw 6.41 update issue blank white window

    Prior to Camera Raw 6.41 update, PSE9 Organize opened and performed as designed. Immediately after the update, and clicking Organize, it opens with an empty white square window appearing to mid right and half outside the PSE9 window. Organizer will e

  • Problem with multiple XP accounts since installing new version of iTunes/QT

    I am running XP Pro. A year or so ago, I went through the "disaster" recovery of installing the new version of iTunes (5.0) along with its nasty QT upgrade that was not ready for prime time. After much thrashing, I got my computer back to iTunes 4.9.

  • Hp mini 311 purchased from verizon

    i have an hp mini 311.   it is running horribly slow and stops and crashes, to the point where it is not hardly usable.  ive run several virus and malward programs and all say its clean..  ive done the obvoius such as deleting cache and temp file... 

  • Issue w/ my Visor Deluxe and syncing:

    All of a sudden, when I turn Palm Desktop on, I get the message "Error- Failed to open Address Book database", and when I click "OK", it turns the Palm Desktop off. I've removed Palm Desktop from my computer abour a 1/2-dozen times and re-installed i

  • Payment terms not derived on RFQ

    We have recently upgraded to ECC 6.0 and now when processing an RFQ to vendor through transaction ME41, the Payment Terms (EKKO-ZTERM) are not automatically derived from the vendor master. I have searched OSS and been unsuccessful at finding this as