Input string
hello sworked? worked hello
output string
I need only work in worked.
I tried with this regex
(?!s)work
But this returns all work in both of sworked? and worked.
To match work not preceded with s use
(?<!s)work
See proof & explanation. (?<!s) is a negative lookbehind disallowing s right before work.
Or, match a word startign with work:
\bwork
See demo. \b is a word boundary.
Add \w* to match the rest of the word if necessary.
In Java, double backslashes (e.g. String regex = "\\bwork";).
Related
I am trying to build a regex pattern and I'm a beginner.
The string looks like this
INITIAL TEXT\KEYWORD1\TEXT1\KEYWORD2\TEXT2\KEYWORD3\TEXT3
The string starts with initial text but the keywords with their texts could be in any order or may not be present.
The initial text could contain any character including backslashes.
I want to capture the initial text so I tried something like this
(?<=(.*)(?=\KEYWORD1\|\KEYWORD2\|KEYWORD3).*)
I am able to capture it on regex101 in group1 but my java code doesn't recognize the group 1.
Thanks for helping.
If the string starts with the text you want to capture, then you can use a start-of-string anchor followed by a lazy match on any character, terminating with a forward lookahead to one of the keywords (or end-of-string, to allow for the case with no keywords):
^.*?(?=\\(?:KEYWORD1|KEYWORD2|KEYWORD3)\\|$)
This will match only the INITIAL TEXT
Demo on regex101
Note that in Java you will need to double the backslash characters in the regex string. Demo on ideone
I would like to match URL strings which can be specified in the following manner.
xxx.yyy.com (For example, the regular expression should match all strings like 4xxx.yyy.com, xxx4.yyy.com, xxx.yyy.com, 4xxx4.yyy.com, 444xxx666.yyy.com, abcxxxdef.yyy.com etc).
I have tried to use
([a-zA-Z0-9]+$)xxx([a-zA-Z0-9]+$).yyy.com
([a-zA-Z0-9]*)xxx([a-zA-Z0-9]*).yyy.com
But they don't work. Please help me write a correct regular expression. Thanks in advance.
Note: I'm trying to do this in Java.
If you want to make sure there is xxx and you want to allow all non whitespace chars before and after. If you want to match the whole string, you could add anchors at the start and end.
Note to escape the dot to match it literally.
^\S*xxx\S*\.yyy\.com$
^ Start of string
\S*xxx\S* Match xxx between optional non whitespace chars
\.yyy Match .yyy
\.com Match .com
$ End of string
Regex demo
In Java double escape the backslash
String regex = "^\\S*xxx\\S*\\.yyy\\.com$";
Or specify the characters on the left and right that you would allow to match in the character class:
^[0-9A-Za-z!##$%^&*()_+]*xxx[0-9A-Za-z!##$%^&*()_+]*\.yyy\.com$
Regex demo
Considering this string below, I want to split a string on a single quote not preceded by \ and not followed by another single quotes('') with regex.
Note: In a string below, those are two consecutive single quotes not double quote.
Java is my 'favorite\''\'' prog' language
I used
split("(?<!\\\\)'")
but does not work. Instead it also takes one quote in every consecutive single quotes preceded by back slash
I want this output
Java is my
favorite\''\'' prog
language
Take a look at lookbehind / lookahead
("(?<![\\\\'])'(?!')")
For this example you also might match a single ' quote and a word boundary \b and the other way around:
'\b|\b'
split("'\\b|\\b' ")
Example Java
Or with a negative lookbehind and negative lookahead taking the whitespace into account:
(?<![\\'])' ?(?!')
split("(?<![\\\\'])' ?(?!')")
Example Java
Credits to #Graciano for the setup of the solution.
From your example I suggest using something similar to
split("[^\\\\']'")
but be careful with corner cases.
I am trying to get following strings:
8374651-37374-19283-284745800
928S-29ED374-872B34-932837
26598TA-297374-CND283-16373
82911-LD391DB-632D-4927831
Looks like this by java regex:
XXXXXXX-XXXXX-XXXXX-284745800
XXXX-XXXXXXX-XXXXXX-932837
XXXXXXX-XXXXXX-XXXXXX-16373
XXXXX-XXXXXXX-XXXX-4927831
and there is no pattern about the length of string between each hyphen.
It could be easy to replace everything except hyphen with X but really difficult to me to exclude last part.
Use string.replaceAll function.
string.replaceAll("[^-\\n](?=.*?-)", "X");
This matches all the characters (but not of a hyphen) which was followed by a hyphen. It won't match the last part since it isn't followed by a hyphen.
DEMO
[^-\n](?=.*-[^-]*$)
Try this.Replace by X.See demo.
https://regex101.com/r/bW3aR1/18
For java it will be
[^-\\n](?=.*-[^-]*$)
The lookahead condition will select only those which are to be replaced by X.
or this pattern, replace w/ x
(?!-).(?=.*-)
Demo
I want to create a regex which matches a word in a String:
MieteĀ 920
I want to match the word "Miete".
My regex:
price.matches("=[\bMiete\b]")
However, it doesn`t work? Pls give me a hint.
If you want to check if some string contains separate word Miete you can use
price.matches(".*\\bMiete\\b.*");
There is no need for = in your regex, also [...] is character class not string literal.
I think your regex is wrong. Try with
price.matches(".*\\bMiete\\b.*")
.* -> 0 or more charcters
\\b -> word boundary
So this will match any string that has Miete surrounded by word boundaries.
EDIT: sorry fixed, I forgot how matching works in Java, I'm more used to Perl :)