Use case for String split - regex

I want code snippet for splitting the below string:
Input : select * from table where a=? and b=? and c=?
Output:
Str1: select * from table where a=?
Str2: and b=?
Str3: and c=?
I do not want to use indexof as of now, whether StringUtils or regex can help me here? I was looking for StringUtilus but I did not get anything in it. Your input is appreciated.

This should suffice:
inputStr.split("(?=\band\b)")

If you are trying it in php then try ,
$myvar = explode('and','select * from table where a=? and b=? and c=? ');
$str1 = $myvar[0];
$str2 = $myvar[1];
$str3 = $myvar[2];

I got, we can use
String str = "select * from table where a=? and b=? anc c=?";
String[] tokens = str.split("\\?");
for (String string : tokens) {
System.out.print("tokens: "+string);
}

Related

Trim String/Text in Flutter

Hi I tried to trim a link in flutter
Currently I am looking into regexp but I think that is not possible
This is the link in full:
http://sales.local/api/v1/payments/454/ticket/verify?token=jhvycygvjhbknm.eyJpc3MiOiJodH
What I am trying to do is to trim the link like this:
http://sales.local/api/v1/payments/454
Kindly advise on best practise to trim string/text in flutter. Thanks!
try to use substring() :
String link = 'http://sales.local/api/v1/payments/454/ticket/verify?token=jhvycygvjhbknm.eyJpc3MiOiJodH';
String delimiter = '/ticket';
int lastIndex = link.indexOf(delimiter);
String trimmed = link.substring(0,lastIndex);
//print(trimmed);
input string print for Flutter:
String str2 = "-hello Friend- ";
print(str2.trim());
Output Print : -hello Friend-
NOte: Here last space remove from string.
1.Right Method:
var str1 = 'Dart';
var str2 = str1.trim();
identical(str1, str2);
2.Wrong Method
'\tTest String is Fun\n'.trim(); // 'Test String is Fun'
main(List<String> args) {
String str =
'http://sales.local/api/v2/paymentsss/45444/ticket/verify?token=jhvycygvjhbknm.eyJpc3MiOiJodH';
RegExp exp = new RegExp(r"((http|https)://sales.local/api/v\d+/\w.*?/\d*)");
String matches = exp.stringMatch(str);
print(matches); // http://sales.local/api/v2/paymentsss/45444
}

Selectively uppercasing a string

I have a string with some XML tags in it, like:
"hello <b>world</b> and <i>everyone</i>"
Is there a good Scala/functional way of uppercasing the words, but not the tags, so that it looks like:
"HELLO <b>WORLD<b> AND <i>EVERYONE</i>"
We can use dustmouse's regex to replace all the text in/outside XML tags with Regex.replaceAllIn. We can get the matched text with Regex.Match.matched which then can easily be uppercased using toUpperCase.
val xmlText = """(?<!<|<\/)\b\w+(?!>)""".r
val string = "hello <b>world</b> and <i>everyone</i>"
xmlText.replaceAllIn(string, _.matched.toUpperCase)
// String = HELLO <b>WORLD</b> AND <i>EVERYONE</i>
val string2 = "<h1>>hello</h1> <span>world</span> and <span><i>everyone</i>"
xmlText.replaceAllIn(string2, _.matched.toUpperCase)
// String = <h1>>HELLO</h1> <span>WORLD</span> AND <span><i>EVERYONE</i>
Using dustmouse's updated regex :
val xmlText = """(?:<[^<>]+>\s*)(\w+)""".r
val string3 = """<h1>>hello</h1> <span id="test">world</span>"""
xmlText.replaceAllIn(string3, m =>
m.group(0).dropRight(m.group(1).length) + m.group(1).toUpperCase)
// String = <h1>>hello</h1> <span id="test">WORLD</span>
Okay, how about this. It just prints the results, and takes into consideration some of the scenarios brought up by others. Not sure how to capitalize the output without mercilessly poaching from Peter's answer:
val string = "<h1 id=\"test\">hello</h1> <span>world</span> and <span><i>everyone</i></span>"
val pattern = """(?:<[^<>]+>\s*)(\w+)""".r
pattern.findAllIn(string).matchData foreach {
m => println(m.group(1))
}
The main thing here is that it is extracting the correct capture group.
Working example: http://ideone.com/2qlwoP
Also need to give credit to the answer here for getting capture groups in scala: Scala capture group using regex

Removing data from string using regular expressions in C Sharp

Definitely I'm not good using regular expressions but are really cool!, Now I want to be able to get only the name "table" in this string:
[schema].[table]
I want to remove the schema name, the square brackets and the dot.
so I will get only the work table
I tried this:
string output = Regex.Replace(reader["Name"].ToString(), #"[\[\.\]]", "");
So you came up with a new idea?? Here is what you can try:
string input = "[schema].[table]";
// replacing the first thing into [] with the dot with empty
string one = Regex.Replace(input, #"^\[.*?\]\.", "");
// or replacing anything before the dot with empty
// string two = Regex.Replace(input, #".*[.]", "");
try this
string strRegex = #"^\[.*?\]\.";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = #"[schema].[table]";
string strReplace = #"";
var result=myRegex.Replace(strTargetString, strReplace);
Console.WriteLine(result);
Why do you want to do replace if you just want to extract part of string?
string table = Regex.Match("[schema].[table]", #"\w+(?=]$)").Value;
It works even in case if you don't have schema.

Remove all the String before :

"\:(.*)$"
Hi all i am using above expression to remove all the string before : (colon), but it is giving me all the string before this. how can i do this. Thanks a lot.
My string is:
This is text: Hi here we go
I am getting: This is text
I want : Hi here we go
Updated code
Sub Main()
Dim input As String = "This is text with : far too much "
Dim pattern As String = "\:(.*)$"
Dim replacement As String = " "
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
Console.WriteLine("Original String: {0}", input)
' MsgBox("Original String: {0}")
Console.WriteLine("Replacement String: {0}", result)
MsgBox("Original String: {0}")
End Sub
Try this pattern. This will help you to match string after colon
/?:(.)/
or
/: (.+)/
It should be:
Dim pattern As String = "(.*)\:"
' in vb if above one doesn't work, then try this one
' Dim pattern As String = "^(.*)\:"
' also i don't think we need to use any brackets here as well.
This regex means, anything before the colon(:), Where you were using anything after the colon(:) in your example.
If you are not dead set on RegEx then you can also use
Dim result As String
result = Strings.Split(Input, ":", 2)(1)
This splits the input into an array with two elements. First element is the text before the first ":", the second element is the text after.

Using NamedParameterJdbcTemplate, how to retrieve record sets from sql server that match a list of regex patterns

I'm new to spring framework. I'm trying to retrieve record sets from a SQL server 2005 database table using NamedParameterJdbcTemplate that matches multiple patterns(patterns are stored in an ArrayList) .
The below code retrieves record sets matching a set of values:
List<String> valuesForBuildingPatterns1 = getValuesForBuildingPatterns1();
List<String> valuesForBuildingPatterns2 = getValuesForBuildingPatterns2();
String sqlQuery = "SELECT * FROM sqlServerTable col1 IN (:pattern1) AND col2 IN (:pattern2)";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("pattern1", valuesForBuildingPatterns1);
params.addValue("pattern2", valuesForBuildingPatterns2);
resultset = namedParameterJdbcTemplate.query(sqlQuery, params, new MyRowMapper());
Problem: Instead of searching for a list of string values, I want to search for a list of regex patterns (like %xyz%). But I'm unable to use both the "IN" clause and "LIKE" operator in the SQL query as shown below:
List<String> pattern1 = new ArrayList<String>();
List<String> pattern2 = new ArrayList<String>();
for(String str : valuesForBuildingPatterns1)
pattern1.add("%"+str+"%"); //If str="xyz", it adds "%xyz%" to the new list
for(String str : valuesForBuildingPatterns2)
pattern2.add("%"+str+"%");
String sqlQuery = "SELECT * FROM sqlServerTable col1 IN LIKE (:pattern1) AND col2 IN (:pattern2)";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("pattern1", pattern1);
params.addValue("pattern2", pattern2);
resultset = namedParameterJdbcTemplate.query(sqlQuery, params, new MyRowMapper());
I also tried looping through the lists and buliding the SQL query with "LIKE" operator alone without the "IN" clause.However the query() method doesn't accept this. Besides, building the SQL query like this beats the main purpose of using NamedParameterJdbcTemplate, I guess.
for(String str1 : valuesForBuildingPatterns1)
for(String str2 : valuesForBuildingPatterns2)
{
String sqlQuery = "SELECT * FROM sqlServerTable col1 LIKE " + str1 + " AND col2 LIKE " + str2";
resultset.add(namedParameterJdbcTemplate.query(sqlQuery,null,new MyRowMapper()));
}
Can you please guide me on solving this problem? Please tell if more information is needed and kindly pardon my ignorance as don't know much about the framework.
Thanks a lot. Your help is greatly appreciated.
PS: The project is being developed using an internal tool developed by the organization that I'm working for. This tool is built upon Spring framework and hence supports all services provided by spring framework.
I figured out a way to achieve it. Thanks guys.
StringBuilder sb = new StringBuilder();
for(String str1 : valuesForBuildingPatterns1)
for(String str2 : valuesForBuildingPatterns2)
sb.append(" col1 LIKE '%" + str1 + "%' AND col2 LIKE '%" + str2 + "%' OR");
sb.delete(sb.length()-2, sb.length());
String sqlQuery = "SELECT * FROM sqlServerTable WHERE" + sb.toString();
resultset = namedParameterJdbcTemplate.query(sqlQuery, params, new MyRowMapper());