How can I write a regex to see if a certain string is contained within two characters.
For example, I want to see which part of the string is contained within a quotation mark.
Java"_virtual_"machine
If I run my regex through this, I want to get _virtual_
How can I achieve this using regexes?
Supposing the text contains only one such pair of characters and there are no escaping tricks or so, you can use this regex
.*"([^"]*)".*
Try it online
I wouldn't do it with regex but with method which finds first and second occurrence of a character so it's more flexible to use in the
String s = "Java\"_virtual_\"machine";
String find = "\"";
int first = s.indexOf(find) + 1;
int second = s.indexOf(find, s.indexOf(find) + 1);
s.substring(first, second);
You can reuse this code on other characters and I bet it's faster than regex.
You can use this regex. I'm not sure if this is the best regex you can use though. It will capture every string between quotes.
.*?"([^"]*)"
Demo here
If you want to get the first string that matches the regex, use :
.*?"([^"]*)".*
Related
I'm trying to learn regex in Java.
So far, I've been trying some little mini challenges and I'm wondering if there is a way to define a nth character.
For instance, let's say I have this string: todayiwasnotagoodday
If I want to replace the third (fourth or seventh) character, how I can define a regex in order to change an specific "index", for this example the 'd' for an empty space "".
I've been searching about it, but so far my implementations match from the first element to the third: ^[a-z]{3}
¿Is it possible to define this regex?
Thanks in advance.
If you want to replace the third character with a space via regex, you could try a regex replace all:
String input = "todayiwasnotagoodday";
String output = input.replaceAll("^(.{2}).(.*)$", "$1 $2");
System.out.println(output); // to ayiwasnotagoodday
Note that you could also avoid regex here, and just use substring operations:
String output = input.substring(0, 2) + " " + input.substring(3);
System.out.println(output); // to ayiwasnotagoodday
Sample Input:
a:b
a.in:b
asds.sdsd:b
a:b___a.sds:bc___ab:bd
Sample Output:
a:replaced
a.in:replaced
asds.sdsd:replaced
a:replaced___a.sds:replaced___ab:replaced
String which comes after : should be replaced with custom function.
I have done the same without Regex. I feel it can be replaced with regex as we are trying to extract string out of specific pattern.
For first three cases, it's simple enough to extract String after :, but I couldn't find a way to deal with third case, unless I split the string ___ and apply the approach for first type of pattern and again concatenate them.
Just replace only the letters with exists next to : with the string replaced.
string.replaceAll("(?<=:)[A-Za-z]+", "replaced");
DEMO
or
If you also want to deal with digits, then add \d inside the char class.
string.replaceAll("(?<=:)[A-Za-z\\d]+", "replaced");
(:)[a-zA-Z]+
You can simply do this with string.replaceAll.Replace by $1replaced.See demo.
https://regex101.com/r/fX3oF6/18
I have a specific requirement to find a pattern and replace the value of matching group(2) in the original string by retaining the pattern(delimiter), I am using the pattern
:(\w+)[:\|]+(.*)
With this pattern it parse the values correctly but i am not able to replace the value of group(2). For example i have a multi-line input string
:20:9405601140
:2D::11298666
:28C:20/1
I want to replace the value(9405601140) of tag 20 with new value(1234) so the output i am expecting is
:20:1234
:2D::11298666
:28C:20/1
Thanks
Use this one:
input = input.replaceAll("(:20):(\\d+)(?!\\d)", "$1:1234");
Here (\\d+)(?!\\d) is checking whether the digits after the :20: are not followed by a digit or not.
However, if you want to replace only the :20:9405601140 there here it is much simple:
input = input.replaceAll(":20:9405601140(?!\\d)", ":20:1234");
You can do this by capturing what you want to keep, instead of what you want to replace, and then using a backreference ($1, for the first capturing group) in the replacement string to include it in the final result.
Something like:
string.replaceAll("(:\\w+[:\\|]+).*", "$11234")
To perform the replacement on all the given lines, or just:
string.replaceAll("(:20[:\\|]+).*", "$11234")
To perform the replacement only on the line beginning with ":20".
try this
s = s.replaceAll("\\A(?::[:\\|])\\w+", "1234");
How about doing it the other way around.
Create a pattern like this (:(\w+)[:\|]+)(.*) then for each row output the first group and your replacement (instead of group 2).
Here is an working example http://ideone.com/9TkGx6
I am new to java, i have a string
"rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23"
What I want is to get the Driving Test word. The word is dynamically changes so what I want to happen is get the word between the rdl_mod_name: and the \n.
Try the following.. It will work in your case..
String str = "rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23";
Pattern pattern = Pattern.compile("rdl_mod_name:(.*?)\n");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Also you can make use of regex,matcher,pattern to get your desired result..
The following links will also give you a fair idea:
Extract string between two strings in java
Java- Extract part of a string between two special characters
How to get a string between two characters?
I would look into java regular expressions (regex). The String matches method uses a regex to determine if there's a pattern in a string. For what you are doing, I would probably use 'matches(rdl_mod_.*\n)'. The '.*' is a wildcard for strings, so in this context it means anything between rdl_mod and \n. I'm not sure if the matches method can process forward slashes (they signify special text characters), so you might have to replace them with either a different character or remove them altogether.
Use java's substring() function with java indexof() function.
Try this code :
String s = "rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23";
String sArr[] = s.split("\n\n");
String[] sArr1 = sArr[1].split(":");
System.out.println("sArr1[1] : " + sArr1[1]);
The s.split("\n\n");will split the string on basis of \n\n.
The second split i.e. sArr[1].split(":"); will split the second element in array sArr on basis of : i.e split rdl_mod_name:Driving Test into rdl_mod_name and Driving Test.
sArr1[1] is your desired result.
I have the following string String string = "attr1 = 45 attr2 =\"82\"";
I am trying to remove all whitespace characters on either side of the = sign.
So that for example my output looks like:
attr1=45 attr="82"
I have tried the following:
String string = "attr1 = 45 attr2 =\"82\"";
string = string.replaceAll("\\s+", "");
I get the following output: attr1=45attr2="82"
Any suggestions would be appreciated.
You don't want to replace all spaces, but only these that are around =. Try with
string = string.replaceAll("\\s*=\\s*", "=");
Note that you cannot do this with just one regex since regexes are designed to match strings, not modify them. Regexes are often used with other tools to perform the later task. In particular, you can use String.replace() or String.replaceAll() with a very simple regex to accomplish your task.
Edit:
If you are still stumped, step back for a minute and think: How would you replace an equal sign with an asterisk, for example? Now can you modify that to do what you actually want?