![]() I created a small snippet to illustrate my problem. How to I overcome it by skipping those entry without dot(.). I will get an error Exception in thread "main" : String index out of range: -1 I want to input the data into a Hashmap and do some datamapping. Far more precise and clean.I have a text file data.txt. Just to make it clear, Coxer's reply is the way to go. Some Text Some Text again Another Text Another Text again But again, since it's inside the lookahead, the new line doesn't get replaced. This will find a comment ended with a new line character or a comment that is at the end of the String. The lookahead finds a new line or the end anchor. It asserts that the #.* is followed by a certain string but doesn't replace that certain string. It looks for whatever is inside this expression but doesn't include it in the text that's matched. This is a positive lookahead and where the regex starts to get tricky. matches any character except a new line.) The start of the comment match: matches a # followed by zero or more characters. IE in ".again #A.", we want to remove that space between n and #. This includes zero or more leading spaces in the comment match. ![]() The individual parts of the expression in the first pass are the following: The first expression replaces comments but not new line characters and the second expression replaces multiple new line characters with a single semicolon. The two-pass circumvents the fact that sometimes you get a duplicate line break. String separator '-' int sepPos str.indexOf (separator) ('Substring after separator '+str.substring (sepPos. Example of list of words to be compared: angry berry you young your apple orange yeast When user entered 'y' character, the results I am getting as text suggestion list: you young your yeast At this point, it is correct. For that, first you need to get the index of the separator and then using the substring () method get, the substring after the separator. I was having some problem when trying to compare strings in Java. I came up with the following which is a two-pass: replaceAll(" *(#.*(?= We want the substring after the first occurrence of the separator i.e. The comments are end line so they can appear in more than one arrangement. However if I use this code I get a indexOutOfBounds exception because 6 is before test. all lines) separating them with " " but I cannot make it work.Ī regex is useful here but it's tricky because your pattern is moderately complex. String html 'This is a test string for example' html.substring(html.indexOf('test'), 6) This is an example of how I would like the code to work, it would make a substring starting at test and stop after 7 characters returning 'test string'. I was thinking of using a StringTokenizer, processing every line, removing text after "#" or ignoring the whole line if it starts with "#", and then printing all tokens (i.e. I'm stuck in finding a way for having the expected output. ReadTextAllInALine.replace(toBeReplaced, "") String toBeReplaced = inputCommandText.substring(startIndex, endIndex) Int endIndex = inputCommandText.indexOf(" ") int startIndex = inputCommandText.indexOf("#") ![]() This code is to ignore all characters after the first "#" but works fine just for the first line if we read it all sequentially. So the output after this is: Some Text Some Text again #A comment #A comment line Another Text Another Text again#Comment ![]() ReadTextAllInALine = readText.replaceAll(" This replaces all newlines with " " readText = userInputTextArea.getText() This should be the expected output: Some Text Some Text again Another Text Another Text again ![]() I need to read this text and ignore all text after "#" symbol. This is a sample input text a user could insert in a textbox: Some Text I'm having some difficulties in excluding part of strings after the "#" symbol. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |