6

How would I do regex matching in Erlang?

All I know is this: f("AAPL" ++ Inputstring) -> true.

The lines that I need to match "AAPL,07-May-2010 15:58,21.34,21.36,21.34,21.35,525064\n"

In Perl regex: ^AAPL,* (or something similar)

In Erlang?

4
  • In this case the example you give WILL match the string so I assume that you are really after a more general case. In that case use the 're' module as has been answered. Commented May 14, 2010 at 12:02
  • If you're just matching the first five exact characters of a string, regex is about the slowest and most complicated way you can do it. Commented May 14, 2010 at 16:55
  • @Dustin the first five characters are dynamic. So what do you suggest? Commented May 15, 2010 at 3:35
  • It's not clear what you mean when you say the characters are dynamic. For the example you've given, there's no language in which I'd use a regex. The match is too simple. Commented May 15, 2010 at 8:30

1 Answer 1

8

Use the re module, e.g.:

...
String = "AAPL,07-May-2010 15:58,21.34,21.36,21.34,21.35,525064\n",
RegExp = "^AAPL,*",
case re:run(String, RegExp) of
  {match, Captured} -> ... ;
  nomatch -> ...
end,
...
Sign up to request clarification or add additional context in comments.

4 Comments

Equivalent to re:run(String, RegExp)
How is this different to running => regexp:first_match(Line, "^AAPL,*" ) ?
Well, it may not be different, but according to the regexp module documentation 'it has been obsoleted by the re module and will be removed in a future release'. So, you should definitely prefer the re module.
just to precise, the match in the case clause is actually {match,ListOfMatchArea} where ListOfMatchArea is in the form [{A,B}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.