regex - How to make an "or" statement in a java Pattern? -
so, , working on program extracts answers html code , stores them array. problem when try make pattern in order separate answers , can't seem make 'or' statement.
the answers stored on html code:
['h','e','l','l','o',' ','w','o','r','l','d']
my problem when write string 1 space(' '
) not recognized pattern, when write file shows helloworld
, no spaces. want pattern simultaneously detects letters , spaces , have no idea of how make 'or' statement in middle of pattern.
this pattern right now, detects letters:
pattern p= pattern.compile("'[a-z]+'");
edit: still doesn't work...do think might else? here's part of code( sorry, know it's mess):
// creates string containing letters separated ' ' public static string createstring(bufferedreader in,bufferedwriter out, string texto) throws ioexception{ stringbuilder sb= new stringbuilder(); pattern p = pattern.compile("'[a-z '']'"); matcher m= p.matcher(texto); while(m.find()){ sb.append(m.group()); } return sb.tostring(); } //splits string in order create array nothing letters public static void toarray(string s, string[] lista, bufferedwriter out) throws ioexception{ lista=s.split("[']"); for(string a:lista){ out.write(a); system.out.print(a); // check output } }
just add space character class:
public class helloworldregex { public static void main(final string... args) { final string regex = "'([a-z ])'"; final pattern pattern = pattern.compile(regex, pattern.case_insensitive); final string input = "['h','e','l','l','o',' ','w','o','r','l','d']"; final matcher matcher = pattern.matcher(input); while (matcher.find()) { system.out.print(matcher.group(1)); } } }
output: hello world
test regex online: https://regex101.com/r/el8ut9/3
Comments
Post a Comment