Parse a string using Regular Expression in java - java

I am trying to parse a String using Regular expression. I have a content text:text and I want to parse the content from a string which has text:text.
Code:
String lines=" from:cal_date_d type:string relationship:many_to_one sql_on:${fact_customer.dw_update_date} = ${cal_date_d.dw_update_date}";
Pattern p = Pattern.compile("(\"?[\\w ]*)\\:(\"?([\\w]*)\"?)");
Matcher m = p.matcher(lines);
while(m.find()) {
String Column_Data=m.group(0);
System.out.println("Regex: "+Column_Data);
}
Ouput:
from:cal_date_d
type:string
relationship:many_to_one
sql_on:
Expected Output:
from:cal_date_d
type:string
relationship:many_to_one
sql_on:${fact_customer.dw_update_date} = ${cal_date_d.dw_update_date}

Try this pattern
([^\s]+( ?= ?[^\s]*)?)
https://regex101.com/r/c0q4W0/2

If you have string like "key1:value1 key2:value2..." then you can use this regex:
([^ ]*:[^ ]*)

Related

How to extract a substring from a sentence until a delimeter

Hi I have some string like this:
location/city/home-a-berlin?/someNewAdress
I want to extract word berlin which placed between "-a-" and "?". How can i do that with regex in java?
I can do it by using string API but kinda stuck with regex.
String cityName = url.substring(url.lastIndexOf("-a-")+3, url.indexOf('?')) //berlin
You can use a capture group with a negated character class.
-a-([^\?]+)\?
Regex demo | Java demo
In Java:
String regex = "-a-([^\\?]+)\\?";
String string = "location/city/home-a-berlin?/someNewAdress\n";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
Output
berlin
Or
s = s.replaceAll(".*-(.*?)\\?.*", "$1");
Alternative regex:
"-a-(.+?)\\?"
Regex in testbench and context:
public static void main(String[] args) {
String input1 = "location/city/home-a-berlin?/someNewAdress";
List<String> inputs = Arrays.asList(input1);
Pattern pattern = Pattern.compile("-a-(.+?)\\?");
List<String> results = inputs.stream().map(s -> pattern.matcher(s))
.filter(Matcher::find).map(m -> m.group(1)).collect(Collectors.toList());
//Output:
results.forEach(System.out::println);
}
Output:
berlin
Summary of regular-expression constructs:
https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html

Java Regular expression for exacted matched case

I have been struggling to find the matched string(s) with Java Regular expression for the syntax {//<some string>/<some String>}
My regular expression should return with these matched cases: {//data/process_id}
Below is the String which i want to find matched syntax:
#process_id={//data/process_id}##history_id={//data/history_id}##Pdataxml={//data/dataxml}##Prules =_UNESCAPEXMLVALUE({//data/rules})##submitted_by={//data/submitted_by}##table_definition={//data/table_definition}
I have tried with below regx pattern but it did not work:
[a-zA-Z_/\\[\\]\\(\\)0-9|]+
Can someone please help me to solve this issue?
You can use the following regex:
\{\/\/[^\/{}\s]*\/[^\/{}\s]*\}
Demo on regex101
code:
String input = "#process_id={//data/process_id}##history_id={//data/history_id}##Pdataxml={//data/dataxml}##Prules =_UNESCAPEXMLVALUE({//data/rules})##submitted_by={//data/submitted_by}##table_definition={//data/table_definition}";
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("\\{\\/\\/[^\\/{}\\s]*\\/[^\\/{}\\s]*\\}").matcher(input);
while (m.find()) {
allMatches.add(m.group());
}
System.out.println(allMatches);
output:
[{//data/process_id}, {//data/history_id}, {//data/dataxml}, {//data/rules}, {//data/submitted_by}, {//data/table_definition}]
Try this regex with a Matcher:
"\\{//([^/]+)/([^/}]+)}"
The parts are captured in groups 1 and 2.
Like this:
Matcher m = Pattern.compile("\\{//([^/]+)/([^/}]+)}").matcher(str);
while (m.find()) {
String part1 = m.group(1);
String part2 = m.group(2);
// do something with the parts
}
To just grab the whole thing, which would be got from m.group(), use this regex:
"(?<=\\{)//[^/]+/[^/}]+(?=})"

How to extract a substring using regex in java

I have the following string :
String xmlnode = "<firstname id="{$person.id}"> {$person.firstname} </firstname>";
How can I write a regex to extract the data inside the {$STRING_I_WANT}
The part I need is without {$} how can I achieve that?
You can use this regex \{\$(.*?)\} with pattern like this :
String xmlnode = "<firstname id=\"{$person.id}\"> {$person.firstname} </firstname>";
Pattern pattern = Pattern.compile("\\{\\$(.*?)\\}");
Matcher matcher = pattern.matcher(xmlnode);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Note : you have to escape each character { $ } with \ because each one is special character in regex.
Outputs
person.id
person.firstname

How split a string using regex pattern

How split a [0] like words from string using regex pattern.0 can replace any integer number.
I used regex pattern,
private static final String REGEX = "[\\d]";
But it returns string with [.
Spliting Code
Pattern p=Pattern.compile(REGEX);
String items[] = p.split(lure_value_save[0]);
You have to escape the brackets:
String REGEX = "\\[\\d+\\]";
Java doesn't offer an elegant solution to extract the numbers. This is the way to go:
Pattern p = Pattern.compile(REGEX);
String test = "[0],[1],[2]";
Matcher m = p.matcher(test);
List<String> matches = new ArrayList<String>();
while (m.find()) {
matches.add(m.group());
}

regex: Java: match word between 2 spaces

How can I extract the "id" from the following string using regex.
string = 11,"col=""book"" id=""title"" length=""10""
I need to be able to extract the "id" header along with the value "title".
outcome: id=""title""
I am trying to the use split function with a regex to extract the identifier from the string.
Try this:
String result = "col=\"book\" id=\"title\" length=\"10\"";
String pattern = ".*(id\\s*=\\s*\"[^\"]*\").*";
System.out.println(result.replaceAll(pattern,"$1"));
Cheers!
Use Pattern and Matcher classes to find what you are looking for. Try to find these regex \\bid=[^ ]*.
String data = "string = 11,\"col=\"\"book\"\" id=\"\"title\"\" length=\"\"10\"\"";
Matcher m = Pattern.compile("\\bid=[^ ]*").matcher(data);
if (m.find())
System.out.println(m.group());

Categories

Resources