Regular expression: Split String
Hi everybody,
I got to split a string when <b>'</b> occurs. But the string should <u>not</u> be splitted, when a <b>?</b> is leading the <b>'</b>. Means: Not split when <b>?'</b>
How has the regular expression to look like?
This does NOT work:
String constHOCHKOMMA = "[^?']'";
<u>Sample-String:</u>
String edi = "UNA'UNB'UNH?'xyz";
Result should be
UNA
UNB
UNH?'xyz
Thanks regards
Mario
Hi
I think u can meke it in two ways
1. using split function u r giving single quote as your delimiter, after each split funcction just add one more split function with delimiter as ?, if it returns true add the previous splited string and next one
2. you are gooing through each and every char of your string and split when next single quote occur, for this u are comparing each of your char with ['] i believe,
just compare the char with '?' if it match ignore next single quote.
Regards
Abhijith YS
Similar Messages
-
Hi All,
What should be the regular expression for string MT940_UB_*.txt and MT940_MB_*.txt to be used as filename inSFTP sender channel in PI 7.31 ??
If any one has any idea on this please let me know.
Thanks
NehaHi All,
None of the file names suggested is working.
I have tried using - MT940_MB_*\.txt , MT940_MB_*.*txt , MT940*.txt
None of them is able to pick this filename - MT940_MB_20142204060823_1.txt
Currently I am using generic regular expression which picks all .txt files. - ([^\s]+(\.(txt))$)
Let me know ur suggestion on this.
Thanks
Neha Verma -
Regular Expressions and String
How do I return a String array as follow using regular expression.
String[] strArray = {"Now is the time", "you can optionally preview your post","message by using a number of special tokens."}
from this string
<separator>Now is the time</separator><separator>you can optionally preview your post</separator><separator>message by using a number of special tokens.</separator>
Note: The string has the <separator> XML tagHow do I return a String array as follow using regular
expression.
String[] strArray = {"Now is the time", "you can
optionally preview your post","message by using a
number of special tokens."}
from this string
<separator>Now is the time</separator><separator>you
can optionally preview your
post</separator><separator>message by using a number
of special tokens.</separator>
Note: The string has the <separator> XML tag
This cannot be done using simple regular expressions - at least not if your number of <separator>s is random, which is what you seem to imply.
Simple regular expressions are one-off, that means it can have a String array as a result, but only to the amount of brackets in the regex.
a regex like:
<separator>([^<]*)</separator><separator>([^<]*)</separator><separator>([^<]*)</separator>
would return what you want, but I doubt that it is as flexible as you want it to be. -
Regular expression - splitting a string
I have a long string that I'm trying to split into a series of substrings. I would like each of the substrings to start with "TTL.. I'm fairly certain that I'm missing something very basic here. I've attached my code which yield NO GROUPS. I didn't see another method for returning the text that the regular expression matched.
String finalLongstring="TTL1,clip1+TTL2+clip3,TTL4,clip4,TTL5,clip5+TTL6+"+
"clip6+TTL7+clip7,TTL8,clip8,TTL9,clip9,TTL10,clip10,TTL11,clip11,TTL12,clip12,"+
"TTL13,clip13+TTL14+clip14,TTL15,clip15,TTL16,clip16,TTL17,clip17,"+
"TTL18,clip18,TTL19,clip19,TTL20,clip20,TTL21,clip21,TTL22,clip22,"+
"TTL23,clip23,TTL24,clip24,TTL25,clip25,TTL26,clip26,TTL27,clip27,"+
"TTL28,clip28,TTL29,clip29"
List<String> chapters = new ArrayList<String>();
chapters.clear();
Pattern chapter=null;
chapter=Pattern.compile("(TTL\\d+([+,]|clip\\d+)*)");
// || | | | | | | |
// || | | | | | | Repeat (commas pluses and clips group) 0 or more times
// || | | | | | one or more digits following 'clip'
// || | | | | clip
// || | | | or..
// || | | plus or comma symbols
// || | group the +, and clip information together
// || one or more digits
// |Match clips starting with TTL
// |
Matcher cp = chapter.matcher(finalLongstring); //NO MATCHES!!
String [] temp = chapter.split(finalLongstring); //temp =EMPTY STRING ARRAY
do{
String chapterPlus=cp.group(1);
if(cp.hitEnd()){break;}
chapters.add(chapterPlus);
}while(true);Thanks in advance for the help.
IcesurferThe main reason your matcher didn't work is because you never told it to do anything. You have to call one of the methods matches(), find() or lookingAt(), and make sure it returns true, before you can use the group() methods. When I did that, your regex worked, but then I modified it to demonstrate a better use of capturing groups, as shown here: import java.util.regex.*;
public class Test
public static void main(String... args)
String str="TTL1,clip1+TTL2+clip3,TTL4,clip4,TTL5,clip5+TTL6+clip6+"+
"TTL7+clip7,TTL8,clip8,TTL9,clip9,TTL10,clip10,TTL11,clip11,TTL12,clip12,"+
"TTL13,clip13+TTL14+clip14,TTL15,clip15,TTL16,clip16,TTL17,clip17,"+
"TTL18,clip18,TTL19,clip19,TTL20,clip20,TTL21,clip21,TTL22,clip22,"+
"TTL23,clip23,TTL24,clip24,TTL25,clip25,TTL26,clip26,TTL27,clip27,"+
"TTL28,clip28,TTL29,clip29";
Pattern p = Pattern.compile("(TTL\\d+)[+,](clip\\d+)[+,]");
Matcher m = p.matcher(str);
while (m.find())
System.out.printf("%6s %s%n", m.group(1), m.group(2));
}The reason your split() attempt didn't work is because the regex matched all of the text; the split() regex is supposed to match the parts you don't want. In fact, it did split the text, creating a list of empty strings, but then it threw them all away, because split() discards trailing empty fields by default.
Finally, the hitEnd() method is not appropriate in this context. It and the requireEnd() method were added to support the Scanner class in JDK 1.5. If you want to see how they work, look at the source code for Scanner, but for now, just classify them as an advanced topic. When you're iterating through text with the find() method, you stop when find() returns false, plain and simple. -
Regular expressions and string matching
Hi everyone,
Here is my problem, I have a partially decrypted piece string which would appear something like.
Partially deycrpted: the?anage??esideshe?e
Plain text: themanagerresideshere
So you can see that there are a few letter missing from the decryped text. What I am trying to do it insert spaces into the string so that I get:
The ?anage? ?esides he?e
I have a method which splits up the string in substrings of varying lengths and then compares the substring with a word from a dictionary (implemented as an arraylist) and then inserts a space.
The problem is that my function does not find the words in the dictionary because my string is only partially decryped.
Eg: ?anage? is not stored in the dictionary, but the word �manager� is.
So my question is, is there a way to build a regular expression which would match the partially decrypted text with a word from a dictionary (ie - ?anage? is recognised and �manager� from the dictionary).I wrote the following method in order to test the matching using . in my regular expression.
public void getWords(int y)
int x = 0;
for(y=y; y < buff.length(); y++){
String strToCompare = buff.substring(x,y); //where buff holds the partially decrypted text
x++;
Pattern p = Pattern.compile(strToCompare);
for(int z = 0; z < dict.size(); z++){
String str = (String) dict.get(z); //where dict hold all the words in the dictionary
Matcher m = p.matcher(str);
if(m.matches()){
System.out.println(str);
System.out.println(strToCompare);
// System.out.println(buff);
If I run the method where my parameter = 12, I am given the following output.
aestheticism
aestheti.is.
demographics
de.o.ra.....
Which suggests that the method is working correctly.
However, after running for a short time, the method cuts and gives me the error:
PatternSyntaxException:
Null(in java.util.regex.Pattern).
Does anyone know why this would occur? -
Regular Expressions and String variables
Hi,
I am attempting to implement a system for searching text files for regular expression matches (similar to something like TextPad, etc.).
Looking at the regular expression API, it appears that you can only match using string variables. I just wanted to make sure this is true. Some of these files might be large and I feel uneasy about loading them into ginormous Strings. Is this the only way to do it? Can I make a String as big as I want?
Thanks,
-MikeNewlines are only a problem if you're reading the
text line-by-line and applying the regexp to each
line. It wouldn't catch expressions that span
lines.
@sabre150: your note re: CharSequence -- so what
you're suggesting is to implement a CharSequence that
wraps the file contents, and then use the regexps on
the whole thing? I like the idea but it seems like
it would only be easy to implement if the file uses a
fixed-width character set. Or am I missing
something...?You are correct for the most basic implementation. It is very easy to create a char sequence for fixed width character sets using RandomAccessFile. Once you go to character sets such as UTF-8 then more effort is required.
While ever the regex is moving forward thought the CharSequence one char at a time there is no problem because one can wrap a Reader but once it backtracks then one needs random access and one will need to have a buffer. I have used a ring buffer for this which seems to work OK but of course this will not allow the regex to move to any point in the CharSequence.
'uncle_alice' is the regex king round here so listen to him.
:-( I should read further ahead next time!
Message was edited by:
sabre150
Message was edited by:
sabre150 -
Regular Expression Fails String replaceAll
I am trying to use regular expressions to replace double backslashes in a string with a single backslash character. I am using version 1.4.2 SDK. Upon invoking the replaceAll method I get a stack trace. Does this look like a bug to anyone?
String s = "text\\\\"; //this results in a string value of 'text\\'
String regex = "\\\\{2}"; //this will match 2 backslash characters
String backslash = "\\";
s.replaceAll(regex,backslash); java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:444)
at java.util.regex.Matcher.appendReplacement(Matcher.java:551)
at java.util.regex.Matcher.replaceAll(Matcher.java:661)
at java.lang.String.replaceAll(String.java:1663)
at com.msdw.fid.fitradelinx.am.client.CommandReader.read(CommandReader.java:55)
at com.msdw.fid.fitradelinx.am.client.CommandReader.main(CommandReader.java:81)
Exception in thread "main"Skinning the cat -
public class Fred12
public static void main(String[] args)
String s = "text\\\\"; //this results in a string value of 'text\\'
String regex = "[\\\\]{2}"; //this will match 2 backslash characters
String backslash = "\\\\";
System.out.println(s.replaceAll(regex,backslash));
String s = "text\\\\"; //this results in a string value of 'text\\'
String regex = "(\\\\){2}"; //this will match 2 backslash characters
String backslash = "\\\\";
System.out.println(s.replaceAll(regex,backslash));
String s = "text\\\\"; //this results in a string value of 'text\\'
String regex = "(?:\\\\){2}"; //this will match 2 backslash characters
String backslash = "\\\\";
System.out.println(s.replaceAll(regex,backslash));
String s = "text\\\\"; //this results in a string value of 'text\\'
String regex = "(?:\\\\)+"; //this will match 2 or more backslash characters
String backslash = "\\\\";
System.out.println(s.replaceAll(regex,backslash));
String s = "text\\\\"; //this results in a string value of 'text\\'
String regex = "\\\\\\\\"; //this will match 2 backslash characters
String backslash = "\\\\";
System.out.println(s.replaceAll(regex,backslash));
} -
Regular Expressions on strings
How would I go about creating a regular expression in LabVIEW that, say, takes an input of a list of numbers (1, 2, 3, 4, 5) and turns it into an array with the elements [1, 2, 3, 4, 5]? If someone could show me example code, I would be quite grateful.
Thanks.
Using LabVIEW 2009 on Windows VistaSpreadsheet String to Array should work for conversion to strings or numbers...
Richard
Attachments:
stringarr.gif 18 KB -
Hi Experts,
I am working on jdev 11.1.1.6.0
I have to validate input text, which user can able to enter alpha number with separated by ','. for example TP103,TP104,TP105........ or 1023,1034,1034....... But they shouldn't enter single ',' or ,,,,,,,,,,,,,,, . Can any one tell me how to write regular expression for this.
Thanks.user can able to enter alpha number with separated by ','.
But they shouldn't enter single ',' or ,,,,,,,,,,,,,,,
Is a ',' required to be validated using a regular expression?
Refer
Lesson: Regular Expressions (The Java&trade; Tutorials &gt; Essential Classes) -
Using a regular expression in String's split method
I have a String that is delimited by commas that I would like to split into a String array. However, the problem is that it is valid that some of the elements could have embeded commas in them. These "extra" commas are always in a predefined pattern though.
For example, the String that I want to parse may look like this (quotes omitted):
data with spaces and tabs , more data with more spaces, now for the problem(5,9), and now some more text and spaces
What I would like the result to be (shown below as if each element has been trimmed):
data with spaces and tabs
more data with more spaces
now for the problem(5,9)
and now some more text and spaces
string.split("[,&&[^[\\(.,.\\)]]]");The idea being that I want to split on all commas except those that are in the pattern (5,9). Where 5 and 9 could be any integer.
The above expression isn't working. It isn't ignoring the comma in the pattern.
Any suggestions?
Thanks.i dunno if this help...
There is a class called the StringTokenizer class where it splits the String and delimiter up..e.g
String s1="Hello,Welcome,Can";
String[ ] arr=new String[3];
To use the StringTokenizer, you create.
StringTokenizer st=new StringTokenizer(s1,",");
//this will split the words, hello,welcome and can into "Tokens"
Then use this method .hasMoreTokens( )
int i=0
while(st.hasMoreTokens( ) ==true) {
arr=st.nextToken( );
i++;
Hope this helps! =) -
Regular Express in String.replaceFirst(..)
I have the following string...
"this ? is what ?."
I am trying to replace the ? with an element. What is a regualr expression that would allow me to replace the just first ? with an item. I tried "?" but it say dangling reference... Ideas? The ?'s will be diffrent so I will use replace first. I could use substring and indexof and concatenate strings but I would perfer not to do so.
String str = "this ? is what ?.";
Object o = "katz";
str = str.replaceFirst("?", o.toString());TIA
Doopsterus? is a special character--a quantifier--that means "match zero or one of the preceding element". If you want to use it literally, you need to escape it with a backslash. Since the backslash is special in Java string literals, you have to escape the backslash so that one gets through to the regex Pattern.
Try this: str = str.replaceFirst("\\?", whatever); -
An additional question about regular expressions with String.matches
does the String.matches() method match expressions when some substring of the String matches, or does it have to match the entire String? So, if i have the String "123ABC", and i ask to match "1 or more letters" will it fail because there are non-letters in the String, but then pass if i add "1 or more letters AND 1 or more digits"? so, in the latter every character in the String is accounted for in the search, as opposed to the first. Is that correct, or are there ways to JUST match some substring in the String instead of the whole thing? i WILL make some examples too... but does that make sense?
It has to match the whole String. Use Matcher.find() to match on just a sub-string()
-
Regular Expressions, split(), and the caret
I have a string delimited by the ^ character:
ITEM1^ITEM2^ITEM3
I've tried using:
split("^")
split("\^")
split("\x5E")
All to no avail. I either get "Invalid escape sequence" or I get the whole string. I'm looking to avoid the StringTokenizer way, just because this is neater IMHO.
Is this possible?\ is the escape character for both Java and regex. The first escape character is for Java, so that the second \ will be treated as a literal in Java and passed as-is to the pattern matcher. The second \ is for the regex, so the pattern matcher will treat the ^ as a literal.
-
Regular expressions in String.replaceAll()
Hi,
I'm trying to implement automatic contextual links on a site.
What I want is to parse a text (containing HTML tags) and replace every occurence of a word by a link.
e.g., the text:
"This is a test text for replacement".
would be replaced by
"This is a <a href="www.test.com">test</a> text for replacement".
Now, obviously I don't want to just do a replaceAll("test", link) because it would also replace things like "contest".
So I'm trying to detect everytime my keyword "test" is not surrounded by literals, which I can do with the regex \Wtest\W.
But how can I use replaceAll() to make the application replace just the central bit and leave whatever non literal stuff is around ?
Or maybe there is a better way of doing this ?
Any idea will be welcome.
ThanksWhy are you makint it so complicated?
public class Test {
public static void main(String[] args) throws Exception {
String text = "This is a test text for replacement";
text = text.replaceAll(" (test) ", " <a href=\"www.test.com\">$1</a> ");
System.out.println("After first pass " + text);
text = text.replaceAll(" (test) ", " <a href=\"www.test.com\">$1</a> ");
System.out.println("After second pass " + text);
}But I agree with Sabre. You probably don't want to do several passes over the same data.
Kaj -
Regular Expressions and string handling
I'd like to be able to make a method that takes a String and removes certain substrings from that string. What I'm doing is that I'm getting some text from the internet (an HTML doc) and I'd like to remove all the tags from it.
my method is:
String DeTokenString(String s)
String t=new String(s.replaceAll("<regex>",""));
return t;
What would I put in place of <regex> to remove all html tags? s has already had its leading and following whitespace removed elsewhere with .trim() before being passed to DeTokenString.
Is there any other simple way to accomplish this?It does on whatever manages to get through my 17
firewall, hand-woven packet destroyers, and
titanium-lead armor.So! That is your final defense mechanism. Bwah hah hah hah hah! Now I have you. That was all I needed to improve my 17-firewall-sneaking-through, hand-woven-packet-destroyer-unweaving, titanium-lead-armor-piercing virus!
� {�
Maybe you are looking for
-
Can i buy a macbookpro with a mastercard debit card?
Can i buy a macbookpro with a mastercard debit card? i will buy it on the spanish store(i'm spanish)
-
Why does my external hard drive disappear?
I've tried USB2, Firewire 800, and network connection (Time Capsule). None of the drives stays connected long enough to get a backup done. The drive just disappears and the backup stops. i tried this with a brand new drive from Western Digital, and a
-
NGS Sponsors authentication does not work in case user has non-English character in his password
Hi, we are using the NAC Guest Server v 2.0.1 and have Sponsors authentication done through Radius servers. Radius servers are Microsoft IAS using AD. Sponsors user authentication works okay in case user's password includes English characters, but do
-
File adapter polling location changing in between the process
Hi, One more scenario, Say File adapter is polling for files in C:\. In between the process, the polling location needs to be changed to D:\. How to do this. Please help. ~rps
-
PDF Preview quality is horrible for iPhoto book!
I created an 82 page iphoto book with several photos and some text and when I preview the order in PDF the quality is distored in various ways every time I try it! I even did a software update and reconfigured iphoto! And the problem then is that thi