Regular Expression To Find A Line Beginning With A Certain Character Using Java?
Jul 12, 2014
I have a text file that has the following lines:
the boy is smart
He is from Australia
** Highly important line
That's all
Now, I need a regular java expression that would match a line in this file that begins with the ** characters. That is, when I match the text with the regex, I should get only this line :
I need a regular expression in java for phone number which that does not allow any character special character only numbers should be allowed.. My sample program is
import java.util.regex.Matcher; import java.util.regex.Pattern; public class c { public String removeOrReplacePhoneNumber(String input) { if (null == input) return input;
I need to parse an html web page to extract specific information from the tags in Java. For example,
<b>Species </b> Strain </td>
I need to look for the Strain info (Strain is variable in length) in the page. The whole web page is stored as a huge string. I need a regular expression that can identify all the Species and retrieve their corresponding strain info. how to do this or can propose some clever string manipulation methods in Java.
I am trying to implement an example (Book* : Java SE 7 ..By S G Ganesh) for validating an IP address but it fails to validate a valid IP addresses. I found another example on the internet(**) and it works super fine, no problem at all. I edited the code (the one I got from internet) into the exact format like book and it still works super but i don't understand why the books' example doesn't work though both look exactly the same now ,further more, how can i compare String x and y for equality?
public class TestClass { void validateIP(String ipStr) { String one = "((25[0-5]|2[0-4]d|[01]?dd?)(.)){3}(25[0-5]|2[0-4]d|[01]?dd?)"; //copied from internet and edited String two = "((25[0–5]|2[0–4]d|[01]?dd?)(.)){3}(25[0–5]|2[0–4]d|[01]?dd?)"; // copied from book String x = "((25[0-5]|2[0-4]d|[01]?dd?)(.))"; String y ="((25[0–5]|2[0–4]d|[01]?dd?)(.))";
I use Eclipse (kepler version, with JDK 7) on MacBook Pro (Lion).I want to overwrite a line printed on my console. I used "" for that. I was expecting the cursor to go to the beginning of the line. Instead, it is going to the next line.
I am currently enrolled in my first Java class. I have taken C# in the past so I've messed with some regular expressions, but I am having trouble finding a good website for Java. Is the syntax the same?I want to create a regular expression to only allow the following characters: c C f F [and any 1-3 digit number].
I have a string "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle" I need to replace the numbers based on the below condition.
if more then 5, replace with many if less then 5, replace with a few if it is 1, replace with "only one"
below is my code, I am missing the equating part to replace the numbers
private static String REGEX="(d+)"; private static String INPUT="We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle"; //String pattern= "(.*)(d+)(.*)"; private static String REPLACE = "replace with many"; public static void main(String[] args) { // Create a Pattern object
I have some problems about performing regular expression. In the following code, the output as a result is being "valid last name". But in my opinion, it should be "invalid last name". Because "Doe" has not any space, apostroph or any hypen. Look at the regular expression statement: "[a-zA-Z]+([ '-][a-zA-Z]+)*"
package deneme; public class Deneme { public static void main(String[] args) { String lastName = "Mc-Something"; // Mc-Something if(!lastName.matches("[a-zA-Z]+([ '-][a-zA-Z]+)*")) System.out.println("Invalid last name"); else System.out.println("Valid last name"); } }
I know that the asterisk qualifier means character(s) in any number.(Is this wrong?) So for "Mc-Something", 'M' corresponds to [a-zA-Z], 'c' corresponds to +, - corresponds to [ '-], 'S' corresponds to [a-zA-Z], "o" corresponds to +, and finally "mething" corresponds to *, doesn't they? But for "Doe", there is no any space, apostroph or hypen.
1) I don't know what regular expression can i use. 2) I want a line to check that preceding line should have //StorageVersion . 3) n then i will check any string between := K[' and '];. From above log file "'706146.0.22106932'" is the value that i am looking for.
I have to read a text looking for value related to certain variable. The variable are always DE plus identifier from 1 to 99 or DE plus identifier from 1 to 99 and SF plus identifier from 1 to 99. The value can be alphanumeric. How can I get these values? As a start point, I try to use split("DE") but I can get something that is wrong if there is "DE" inside the text that doesn't me interest. I look for scanner but it doesn't work. I guess that there is some way, maybe using regex but I am completely lost ( I have never used regedix before and I am in rush to fix this).
Basically, the text is similar to the below where DE means data element and sf sub field. Some data elements have sub fields while others don't. I guess that there is a way to split with something like DE+anyNumberFrom1To99 = theValueAimed in some array and DE+anyNumberFrom1To99+,+SF+anyNumberFrom1To99 = theValueAimed in other array.
DE 2, SF 1 = 00 SOME TEXT THAT DOESN'T INTEREST ME DE 22, SF 1 = 0 SOME TEXT THAT DOESN'T INTEREST ME DE 22, SF 4 = 1 SOME TEXT THAT DOESN'T INTEREST ME DE 22, SF 5 = 0 SOME TEXT THAT DOESN'T INTEREST ME DE 22, SF 6 = 11 SOME TEXT THAT DOESN'T INTEREST ME DE 22, SF 7 = 90x SOME TEXT THAT DOESN'T INTEREST ME DE 22, SF 7 = 12ab SOME TEXT THAT DOESN'T INTEREST ME DE 99 = 1234 SOME TEXT THAT DOESN'T INTEREST ME
Regular expression which I want to use to split a string. The string could look similar to this:
"a = "Hello World" b = "Some other String" c = "test""
The String is read from a file where the file contents would be:
a = "Hello World" b = "Some other String" c = "test"
After splitting I would like to get the following array:
String[] splitString = new String[] {"a", "=", ""Hello World"", "b", "=", ""Some other String"", "c", "=", ""test""}
I know I could just write a simple text parser to go over the string character by character, but I would rather not do it to keep my code clean and simple. No need to reinvent the wheel.
However, I just cant seem to be able to find the right regular expression for this task. I know that a RE must exist because this can be solved by a finite automaton.
My requirement is to find the line number using multiline string. Here I need to extract the string between FROM and where clause(from the below string) and need to find the line number in the file
SELECT HL.LOCATION_ID,HPS.PARTY_SITE_ID,HCAS.CUST_ACCT_SITE_ID INTO LN_SITE_LOCATION_ID,LN_LOC_PARTY_SITE_ID,LN_CUST_ACCT_SITE_ID FROM HZ_LOCATIONS HL, HZ_PARTY_SITES HPS,
I need to know why this program is giving me error (illegal start of expression). What must i do to stop the error?
//Read a character from keyboard class Kbin{ public static void main(String[]args){ throws java.io.IOException{ char ch;
System.out.print( " Press a key followed by ENTER: "); ch = (char) System.in.read();//read character from keyboard System.out.println( "Your key is: " + ch); } } }
I'm creating a program that searches a txt file for a given string, then return the number line and the line itself. However, my testFile class isn't detecting my searchWord methods.
The searchWord and recursiveSearch is written in a java class called BasicFile
public List<String> searchWord(String key) throws Exception { LineNumberReader lnr = new LineNumberReader(new FileReader(f)); return recursiveSearch(lnr.readLine(), key, lnr); } public List<String> recursiveSearch(String currentLineText, String key, LineNumberReader lnr)
[code]....
Is it because I'm using a list instead of a string?
I'm working on a program and can't seem to fix my for loop error.I get an error at the for loop line saying illegal start of expression and i don't know how to fix it:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
public static void main(String[] args) throws FileNotFoundException, IOException{ // TODO code application logic here FileReader fr = new FileReader("gautam.txt"); BufferedReader br = new BufferedReader(fr); int line = 0; int word = 0; int character = 0; String s; while((s=br.readLine())!=null)
I need creating the end of line character on line 2 of the attached image.
When I try with
and
I get the end of line symbol on line 1. I tried converting ASCII 10 to a character and putting that in the file, but I still get the end of line symbol on line one.