string-parsing

preg_match array items in string?

我怕爱的太早我们不能终老 提交于 2019-11-28 13:05:53
Lets say I have an array of bad words: $badwords = array("one", "two", "three"); And random string: $string = "some variable text"; How to create this cycle: if (one or more items from the $badwords array is found in $string) echo "sorry bad word found"; else echo "string contains no bad words"; Example: if $string = "one fine day" or "one fine day two of us did something" , user should see sorry bad word found message. If $string = "fine day" , user should see string contains no bad words message. As I know, you can't preg_match from array. Any advices? How about this: $badWords = array('one'

Splitting on spaces, except between certain characters

陌路散爱 提交于 2019-11-28 11:42:30
I am parsing a file that has lines such as type("book") title("golden apples") pages(10-35 70 200-234) comments("good read") And I want to split this into separate fields. In my example, there are four fields: type, title, pages, and comments. The desired result after splitting is ['type("book")', 'title("golden apples")', 'pages(10-35 70 200-234)', 'comments("good read")] It is evident that a simple string split won't work, because it will just split at every space. I want to split on spaces, but preserve anything in between parenthesis and quotation marks. How can I split this? This regex

Parsing non-standard JSON

百般思念 提交于 2019-11-28 09:44:39
问题 Anyone know what type of JSON (if even that!) the following code is? I'm retrieving this from the HTML of a website. I'm trying to parse it in C# with a JSON parser, but I'm having to do lots of preparatory editing to format it as 'valid' JSON according to JSONLint. For example, the names of the variables should all have double quotes rather than having no quotes at all. { status: 'A', displayed: 'Y', start_time: '2010-11-2600: 00: 00', start_time_xls: { en: '26thofNov201000: 00am', es:

What's the best way to check if a character is a vowel in Java?

别来无恙 提交于 2019-11-28 09:34:19
问题 I'm trying to check if a certain char is a vowel. What's the best way to go about doing this? 回答1: Here's the solution I've been using for a while, and it hasn't let me down yet: private static String VOWELS = "AÀÁÂÃÄÅĀĂĄǺȀȂẠẢẤẦẨẪẬẮẰẲẴẶḀÆǼEȄȆḔḖḘḚḜẸẺẼẾỀỂỄỆĒĔĖĘĚÈÉÊËIȈȊḬḮỈỊĨĪĬĮİÌÍÎÏIJOŒØǾȌȎṌṎṐṒỌỎỐỒỔỖỘỚỜỞỠỢŌÒÓŎŐÔÕÖUŨŪŬŮŰŲÙÚÛÜȔȖṲṴṶṸṺỤỦỨỪỬỮỰYẙỲỴỶỸŶŸÝ"; private static boolean isVowel(char c) { return VOWELS.indexOf(Character.toUpperCase(c)) >= 0; } For my applications, it's reasonably fast. 回答2:

How to separate full name string into firstname and lastname string?

狂风中的少年 提交于 2019-11-28 05:23:44
I need some help with this, I have a fullname string and what I need to do is separate and use this fullname string as firstname and lastname separately. This will work if you are sure you have a first name and a last name. string fullName = "Adrian Rules"; var names = fullName.Split(' '); string firstName = names[0]; string lastName = names[1]; Make sure you check for the length of names . names.Length == 0 //will not happen, even for empty string names.Length == 1 //only first name provided (or blank) names.Length == 2 //first and last names provided names.Length > 2 //first item is the

java generic String to <T> parser

佐手、 提交于 2019-11-28 03:26:32
问题 Is there a straight-forward way to implement a method with the following signature? At minimum, the implementation would need to handle primitive types (e.g. Double and Integer). Non-primitive types would be a nice bonus. //Attempt to instantiate an object of type T from the given input string //Return a default value if parsing fails static <T> T fromString(String input, T defaultValue) Implementation would be trivial for objects that implemented a FromString interface (or equivalent), but I

Parse time of format hh:mm:ss [closed]

安稳与你 提交于 2019-11-27 23:25:00
How can I parse a time of format hh:mm:ss , inputted as a string to obtain only the integer values (ignoring the colons) in java? As per Basil Bourque's comment, this is the updated answer for this question, taking into account the new API of Java 8: String myDateString = "13:24:40"; LocalTime localTime = LocalTime.parse(myDateString, DateTimeFormatter.ofPattern("HH:mm:ss")); int hour = localTime.get(ChronoField.CLOCK_HOUR_OF_DAY); int minute = localTime.get(ChronoField.MINUTE_OF_HOUR); int second = localTime.get(ChronoField.SECOND_OF_MINUTE); //prints "hour: 13, minute: 24, second: 40":

Convert String with Dot or Comma as decimal separator to number in JavaScript

和自甴很熟 提交于 2019-11-27 21:47:28
An input element contains numbers a where comma or dot is used as decimal separator and space may be used to group thousands like this: '1,2' '110 000,23' '100 1.23' How would one convert them to a float number in the browser using JavaScript? jQuery and jQuery UI are used. Number(string) returns NaN and parseFloat() stops on first space or comma. hytest Do a replace first: parseFloat(str.replace(',','.').replace(' ','')) Shafiq The perfect solution accounting.js is a tiny JavaScript library for number, money and currency formatting. Check this for ref I realise I'm late to the party, but I

Javascript math parser library

余生颓废 提交于 2019-11-27 18:09:27
问题 Is there a good math parser in Javascript? I want to be able to parse something like: LOG(3.14)+5^2+POW(2,LN(X*2,Y)) Thanks, 回答1: Here is a brand new initiative: http://mathjs.org Comes with an extensive and easy to use parser which also supports assignment and usage of variables and functions like in your example expression. 回答2: Use this one. It defined an "operator" object that lets you define your own operators. http://jsfromhell.com/classes/math-processor Warning: it uses with . If you

Safe integer parsing in Ruby

大兔子大兔子 提交于 2019-11-27 17:08:47
I have a string, say '123' , and I want to convert it to 123 . I know you can simply do some_string.to_i , but that converts 'lolipops' to 0 , which is not the effect I have in mind. I want it to blow up in my face when I try to convert something invalid, with a nice and painful Exception . Otherwise, I can't distinguish between a valid 0 and something that just isn't a number at all. EDIT: I was looking for the standard way of doing it, without regex trickery. Slartibartfast Ruby has this functionality built in: Integer('1001') # => 1001 Integer('1001 nights') # ArgumentError: invalid value