I have a string which I want a string to parse via Java or Python regexp:
something (\var1 \var2 \var3 $var4 #var5 $var6 *fdsfdsfd #uytuytuyt fdsgfdgfdgf aaabbccc)
The number of var is unknown. Their exact names are unknown. Their names may or may not start with "\" or "$", "*", "#" or "#" and there're delimited by whitespace.
I'd like to parse them separately, that is, in capture groups, if possible. How can I do that? The output I want is a list of:
[\var1 , \var2 , \var3 , $var4 , #var5 , $var6 , *fdsfdsfd , #uytuytuyt , fdsgfdgfdgf , aaabbccc]
I don't need the java or python code, I just need the regexp. My incomplete one is:
something\s\(.+\)
something\s\((.+)\)
In this regex you are capturing the string containing all the variables. split it based on whitespace since you are sure that they are delimited by whitespace.
m = re.search('something\s\((.+)\)', input_string)
if m:
list_of_vars = m.group(1).split()
I am totally new to reg-ex and I want to get validation for the string for valid combination of logical operators like ( ! , & , ( , ) , | ) . for Example if & | combined than it should be invalid as AND OR should come together. likewise possible invalid combination are &|, |& , (), !& ,&! etc
like example of below String
1. (ABC)&(DFG)|!(ZXC) - pass because all operators are correctly combined
2. !(ABC|DKJ)&VBN - pass
3. !(ADF&(!&(BER|CTY))|DGH) = failed as !& combined
4. !(ABC&DKJ)&|VBN - failed as & | combined
I know their several ways like I can use String's contains method to get check and reject if not passed the validation. But I am looking for solution through reg-ex in java
Just to avoid matching invalid operator combos you can use negative lookahead regex like this:
^(?!.*?(&\\||\\|&|\\(\\)|!&|&!))
Use it with MULTILINE option like this for multiline inputs:
Pattern p = Pattern.compile( "(?m)^(?!.*?(&[!|]|[(|]&|\\(\\)))" );
RegEx Demo
For using it with a string input you can do:
boolean value = input.matches( "(?!.*?(&[!|]|[(|]&|\\(\\))).+" );
I have a txt file that contains the following
SELECT TOP 20 personid AS "testQu;otes"
FROM myTable
WHERE lname LIKE '%pi%' OR lname LIKE '%m;i%';
SELECT TOP 10 personid AS "testQu;otes"
FROM myTable2
WHERE lname LIKE '%ti%' OR lname LIKE '%h;i%';
............
The above query can be any legit SQl statement (on one or multiple lines , i.e. any way user wishes to type in )
I need to split this txt and put into an array
File file ... blah blah blah
..........................
String myArray [] = text.split(";");
But this does not work properly because it take into account ALL ; . I need to ignore those ; that are within ";" AND ';'. For example ; in here '%h;i%' does not count because it is inside ''. How can I split correctly ?
Assuming that each ; you want to split on is at the end of line you can try to split on each ; + line separator after it like
text.split(";"+System.lineSeparator())
If your file has other line separators then default ones you can try with
text.split(";\n")
text.split(";\r\n")
text.split(";\r")
BTW if you want to include ; in split result (if you don't want to get rid of it) you can use look-behind mechanism like
text.split("(?<=;)"+System.lineSeparator())
In case you are dynamically reading file line-by-line just check if line.endsWith(";").
I see a 'new line' after your ';' - It is generalizable to the whole text file ?
If you must/want use regular expression you could split with a regex of the form
;$
The $ means "end of line", depending of the regex implementation of Java (don't remember).
I will not use regex for this kind of task. Parsing the text and counting the number of ' or " to be able to recognize the reals ";" delimiters is sufficient.
My console prints value of String result as
[{"series":[ {\"data\" : [ 100.0 , 222.0 , 555.0 , 367.0 , 100.0]}],"yAxis":{\"plotLines\":[{\"dashStyle\":\"solid\",\"color\":\"black\",\"width\":\" 2.0\",\"value\":\" 27.0\",\"label\":{\"text\":\"Average\"}}],\"title\":{\"text\":\"FTFR\"}},"title":{\"text\":\"First Time Fix Rate\"},"legend":{\"backgroundColor\":\"#FFFFFF\"},"chart":{\"type\":\"line\"},"xAxis":{\"title\":{\"text\":\"Time(Years)\"},\"categories\":\[2009 , 2010 , 2011 , 2012 , 2013]}}]"
I need to parse the String result as valid JSON
but the following code throws exception:
JSONParser parser = new JSONParser();
Object obj = parser.parse(result);
Exception:
org.jboss.resteasy.spi.UnhandledException: Unexpected character (\) at
position 14.
How can i parse this kind of string to proper JSON?
You cannot use backslashes before double quotes. This is not correct JSON.
You shouldn't have \". Are you pasting this result into an IDE and you then try to parse it? (Netbeans adds \ to any quotation marks which are within another set of quotation marks.
If on the other hand, this is exactly the type of string you are getting, you would need to do something like so:
String str = "[{\"series\":[ {\\\"data\\\" : [ 100.0 , 222.0 , 555.0 , 367.0 , 100.0]}],\"yAxis\":{\\\"plotLines\\\":[{\\\"dashStyle\\\":\\\"solid\\\",\\\"color\\\":\\\"black\\\",\\\"width\\\":\\\" 2.0\\\",\\\"value\\\":\\\" 27.0\\\",\\\"label\\\":{\\\"text\\\":\\\"Average\\\"}}],\\\"title\\\":{\\\"text\\\":\\\"FTFR\\\"}},\"title\":{\\\"text\\\":\\\"First Time Fix Rate\\\"},\"legend\":{\\\"backgroundColor\\\":\\\"#FFFFFF\\\"},\"chart\":{\\\"type\\\":\\\"line\\\"},\"xAxis\":{\\\"title\\\":{\\\"text\\\":\\\"Time(Years)\\\"},\\\"categories\\\":\\[2009 , 2010 , 2011 , 2012 , 2013]}}]\"";
System.out.println(str.replaceAll("\\\\\"", "\"").replaceAll("\\\\\\[","["));
Yields:
[{"series":[ {"data" : [ 100.0 , 222.0 , 555.0 , 367.0 , 100.0]}],"yAxis":{"plotLines":[{"dashStyle":"solid","color":"black","width":" 2.0","value":" 27.0","label":{"text":"Average"}}],"title":{"text":"FTFR"}},"title":{"text":"First Time Fix Rate"},"legend":{"backgroundColor":"#FFFFFF"},"chart":{"type":"line"},"xAxis":{"title":{"text":"Time(Years)"},"categories":[2009 , 2010 , 2011 , 2012 , 2013]}}]"
In your case you might want to do away with two of the slashes in each case, I had to make them so that I can execute it on my IDE.
You could do something like
Object obj = parser.parse(result.replace("\\", ""));
to remove all your \ but it looks like a terrible hack. (By the way replacing the sequence \" by " shouldn't be enough because there is also a \[ that should make a problem.)
How do you get that string in your console?
It seems that the escape characters are not even systematically present.
Agree with npinti You might be trying to use the backslash as the escape character but it should not be visible when you print it. So probably you might have escaped the escape character. It is bit funny when you say it like that..:)
I am wondering with this behavior. In my application I am getting data from server , or my own created database. ( I clone server database)
.replaceAll ( "\r\n" , "<br/>" ) ;
When the data is come from server that it replace. But When data is get from sqlite database its unable to replace the above. As I have try .replaceAll ( "a" , "??" ) ; and its working.
The database data is
Bradley Ambrose is the freelance cameraman who recorded the John Key and John Banks tea meeting.\r\n\r\nHe intentionally placed a black bag with a recording device on the table where Key and Banks were sitting, although he claims it was a mistake, If that were true then how did so many people get a copy of it???\r\n\r\nAlso this guy bloody changed his name from Brad White what the hell is this guy an international man of mystery or something.
I have also debug that issue in detail. But the is not replaced even code is executed the above line successfully.
I have also try
replaceAll ( "\n" , "<br/>" )
replaceAll ( "\r" , "<br/>" )
There is debugging picture.
Does the input string contain actual CR and LF characters or pairs of \ and r and \ and n?
The regex won't work in latter case. It would require .replaceAll("\\\\r\\\\n" , "<br/>")
Can you try with Pattern#quote() ?
Something like:
System.out.println("hello\r\n\r\n something".replaceAll(Pattern.quote("\r\n"), ""));
The code is fine. The data you are seeing in the debug screen is wrong. Do the same debug session and insert a system.out.println and check the output with the output in the debug screen.
Unless you you mean the database actually has the string "\r\n". The above assumes that the database actually contains the carrige return and line feed characters. If your database actually has the backslash character followed by the 'n' character then your regex needs a simple tweak. s.replaceAll("\\\\r\\\\n", "")