How to write a regex for this expression [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i need to write a regex which only lets the (ä,ö,ü,..) int the array..the code bellow also lets white spaces in the array..example parts2[0] and parts2[1] has white spaces but supposed to be ö
String str="geliyoru mamaadsödsödösdä dädsdäsfäfsä yüwüsüdsüfsäfsfä"
String[] parts2 = str.split("[A-Za-z]+|[0-9]+|x[A-Fa-f0-9]");
StringBuilder result = new StringBuilder(str.length());
for (int i = 0; i < parts.length; i++) {
System.out.println(parts2[i]);
result.append(parts[i]);
}

You can use the Pattern.replaceAll feature of Java.
String str= "geliyoru mamaadsödsödösdä dädsdäsfäfsä yüwüsüdsüfsäfsfä";
Pattern p = Pattern.compile("[^\u00E4\u00F6\u00FC]*");
String result = p.matcher(str).replaceAll("");
In the pattern between [^ and ] you should enumerate all the characters you wish to preserve using the unicode definition. You can look them op for instance using Google/DuckDuckGo. The ^ part between the square brackets meens that you invert the matching. Thus all character not present in the group are matched. And in the last line all these characters are replaced by empty strings.
For performance, I've added * at the end of the regex, such that groups of characters are matched as one and thus replacement is done only once.
You can see a jdoodle here

Related

String a[] = s.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am confuse in the logic behind the code (?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)")
it is separating numbers and alphabets like input String abc12dc23 then it is spliting it as output abc 12 dc 23.
I just want the explanation how the above code is working?
This regex:
(?<=\D)(?=\d)|(?<=\d)(?=\D)
matches 2 kinds of patterns, as suggested by the | character:
This pattern:
(?<=\D)(?=\d)
and this pattern:
(?<=\d)(?=\D)
The former looks for a position in the string where there is a non-digit (\D) character before that position and a digit (\d) after it. The latter looks for a position where the reverse happens, a digit before and a non-digit after.
To say this in a more abstract way, the regex is looking for digit-non-digit boundaries.
The split method looks for all occurrences of the pattern and splits the string when it finds one.

Is it possible to match any number of specific characters within a term? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have some user input that I am trying to match against however I strip out special characters such as '^- etc. I would like to know if it is possible to provide a specific set of characters to match or "ignore". So if the list contained "^-" and the term was PRM, P^^R---^M would be a match but PROM would not since O is not in the set of "allowed" characters.
Is this possible with regex?
With Java 8, you can use String.join to join the individual characters from the term with character groups representing one or more of your special characters, i.e. something like [^-]*, but with regex special character escaped.
String term = "PRM";
String special = "^-";
String delimiter = "[" + Pattern.quote(special) + "]*";
String regex = String.join(delimiter, term.split(""));
System.out.println(Pattern.matches(regex, "PROM")); // --> false
System.out.println(Pattern.matches(regex, "P^^R---^M")); // --> true
Alternatively, you could also replaceAll the special chars with "" and check for equality:
System.out.println(term.equals("PROM".replaceAll(delimiter, ""))); // --> false
System.out.println(term.equals("P^^R---^M".replaceAll(delimiter, ""))); // --> true

Splitting a comma-separated string but ignoring commas in whole word [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i have a string like:
String test = "firstName:a,lastName:b,addressOne:line 1,line 2,city:other";
i am trying to do test.replaceAll(",","\",\"").I mean replace , with ",". I want to this only for the whole strings. For eg addressOne is a single string with comma seperated i dont want to replace that. Is there any regex or someother way i can do this?
i should get a string like
"firstName":"a","lastName":"b","addressOne":"line 1 , line 2","city":"other"
after replace , but i am getting
"firstName":"a","lastName":"b","addressOne":"line 1 "," line 2","city":"other".
You can split your string with commas that are followed by a word which has been followed by :. And for this aim you can use a positive look-ahead and for matching the leading word use a negated character class [^:,]+ which will match any string except : and ,:
test.split(",(?=[^:,]+:)")

How can I read this string in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a string such as this: "xxxxxxx , yyyyyyy - zzzzzz"
and sometimes it happens to be "xxxxxxx - zzzzzz"
the length of x may vary.
What I want to get, is always the x word.
Is there an easy way to do this?
What I thought was:
Iterate through the string, append to a stringbuilder every char I read until I read a ",", then break the iteration and I get the word, but this looks pretty messy.
So maybe there is an easier way to do this.
Here are several ways to get the first character:
String str = "x , y - zzzzzz";
System.out.println(str.charAt(0));
System.out.println(str.substring(0, 1));
System.out.println(str.replaceAll("^(.).*", "$1"));
See IDEONE demo
Choose the one you like more :)
UPDATE:
To find the first word, you may use the following regex pattern:
String pattern = "[\\s\\p{P}]+";
\s stands for whitespace, and \p{P} stands for punctuation.
You can use split with this as in
String str = "xxxxxxx , yyyyyyy - zzzzzz";
System.out.println(str.split(pattern)[0]);
str = "xxxxxxx - zzzzzz";
System.out.println(str.split(pattern)[0]);
str = "xxxxxxx, zzzzzz";
System.out.println(str.split(pattern)[0]);
str = "xxxxxxx-zzzzzz";
System.out.println(str.split(pattern)[0]);
All will output xxxxxxx. However, if you need to restrict the punctuation to some smaller subset, either exclude them in the &&[^-] negated subtraction:
[\\s\\p{P}&&[^-]]+
Or just define your own range:
[\\s.,;:]+
Just another demo

Pattern matching for validating the input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to validate the input as
Asterisks are permitted in positions 2-5.
Position One should be alphabetic (except for the ~)
-No characters should accept numbers.
Other than the exceptions mentioned above, no special characters are allowed
I am trying to build as this.
final Pattern pattern =
Pattern.compile("^[a-zA-Z~][a-zA-Z*]*$", Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(this.mainStaOrgBO.getStaOrgCode());
final boolean specialCharCheck = matcher.find();
if (specialCharCheck) {
}
How about:
^[a-zA-Z~][a-zA-Z*]{1,4}[a-zA-Z]*$
Explanation:
^ : start of string
[a-zA-Z~] : First char can be letter or ~
[a-zA-Z*]{1,4} : char 2 to 5 can be letter or *
[a-zA-Z]* : rest of string only letter
$ : end of string.
This should work
[a-zA-Z~]\*[a-zA-Z~]{2}\*[a-zA-Z~]*
If the * are optional
[a-zA-Z~][a-zA-Z~\*][a-zA-Z~]{2}[a-zA-Z~\*][a-zA-Z~]*
Test is here Online Java Regex Test

Categories